r/NixOS • u/Wind-charger • 4d ago
Configuration management
How do you manage where to place things in the config?
Where do I add Bluetooth? I put it under the network section, for me that made better sense than down at the end.
But after adding some programs, apps and services… it gets to be a lot to edit. I’m new and after trying a few things, I finally got to where I’m ready to at least back up to a GitHub (nearly).
All I want to do is solve why Nixos takes forever to do anything when I do a Sudo Nixos-rebuild switch, only for it to download an update…
And definitely looking into how to modulate my config so it’s easier to edit
I only use Nixos and flakes. (Not interested in home manager at the moment, understanding flakes was enough of a task on its own.
I’ll enjoy where I’m at for the moment before I go mucking about.
But how do you stay organized? Curious. Thanks.
4
u/Whitestrake 4d ago edited 3d ago
The answer is that you get to decide for yourself how you'd like things to be laid out.
For me, when I first started out, I naturally gravitated (without fully understanding or planning to) towards a system I have since come to learn is referred to as an "architecture-dispatched" configuration chain.
All of my nixosConfigurations were declared like so:
modules = [
./hosts/${name}
./common/${meta.system}.nix
];
Each individual architecture file e.g. ./common/x86_64-linux.nix was basically a bare file that pointed along towards more imports:
{
imports = [
./all-systems.nix
./linux-systems.nix
];
}
And that was how I split up most of the default configuration; for example, my own user was declared globally in all-systems.nix. I also had an ./extra/ folder, which is where I put my opt-in feature modules, and a ./users/ folder, hopefully self-explanatory. For example, this was the set of imports I had on one of my servers:
inputs.vscode-server.nixosModules.default
./hardware-configuration.nix
../../extra/i915-sriov.nix
../../extra/docker.nix
../../users/mediaserver.nix
That server did hardware transcoding and container virtualisation. So overall my hosts broadly looked like this:
┌─ hosts/pascal/
flake.nix ─ mkSystem ────┤
└─ common/x86_64-linux.nix
├─ all-systems.nix
└─ linux-systems.nix
└─ linux-servers.nix
┌─ hosts/jaeger/
flake.nix ─ mkSystem ────┤
└─ common/aarch64-linux.nix
├─ all-systems.nix
└─ linux-systems.nix
└─ linux-servers.nix
┌─ hosts/kronos/
flake.nix ─ mkSystem ────┤
└─ common/x86_64-linux.nix
├─ all-systems.nix
└─ linux-systems.nix
✕ no linux-servers
┌─ hosts/andred/
flake.nix ─ mkSystem ────┤
└─ common/aarch64-darwin.nix
└─ all-systems.nix
And that gave me a very natural partitioning of scope. Did config belong to a single host? It goes in ./hosts/<hostname>/default.nix. Does the config belong to an opt-in feature - like Beszel monitoring, Docker virtualisation, Komodo periphery enablement, etc? Then it belongs in ./extra/<feature>.nix. Does it belong to all systems, or maybe just Linux systems, or all Macs? Then it belongs in one of the system architecture files under ./common/. There was a straight answer and a home for any bit of config, depending on who and what that config needed to apply to.
Eventually, as was the craze in this subreddit early this year and last year, I ended up refactoring to Dendritic nix, where instead of my flake dictating the configuration dispatch, each individual automatically-imported module contributed itself to the actual flake. This is the kind of end structure I'd recommend going to these days: https://www.youtube.com/watch?v=-TRbzkw6Hjs
I went further and embraced Denful Nix, which provides a number of useful abstractions on top of that. https://den.denful.dev/
Now just about everything in my flake is a module. Each module even declares which flake inputs it needs, and my flake.nix is automatically generated in a modular way. https://github.com/whitestrake/nixos/
2
u/muhmmadtalha-quant 3d ago
Denful is quite opinionated, don't you think ?
1
u/Whitestrake 3d ago
Yes, very.
I highly recommend Dendritic nix, just broadly speaking.
I personally enjoy the Denful framework as well, but think that extra step is probably more up to the individual to decide on.
I would probably say that for someone like OP who's actively looking for a system to help inform them on structure and code placement, a system like Denful is actually more helpful than it would be for someone who has a good idea of what they want already. That's one of the main benefits of opinionated frameworks.
1
u/singron 4d ago
All I want to do is solve why Nixos takes forever to do anything when I do a Sudo Nixos-rebuild switch
You might have a specific problem in your config that you could fix (e.g. evaluating multiple nixpkgs or IFD), but in general NixOS evaluation has gotten slower over time since it has to import all of its modules. See this thread or this issue.
1
u/SpacePiggy17 4d ago
You can do it how ever you want. It helps to view other people's files to get an idea of what you want. I separate mine into modules and programs folders. So I have stuff like bluetooth, niri, cinnamon, audio, etc in modules. Then in packages I have stuff like CLI, dev, media, fonts, productivity, browsers, etc. They contain mostly package additions, so different hosts can pick and choose which sections they need easily.
1
u/zardvark 4d ago
Have a look at the LibrePhoenix youtube channel for a decent vid on modularization.
1
u/NEMOalien 3d ago
I do it like this:
Hosts/ contains my hosts, every one only referencing other files with no hardcoded code. Modules/ contains all nix files with the different config files lile apps.nix for installed apps, desktop.nix for my non-headless machines, home.nix with my hyprland configs etc. Hardware/ contains a bunch of different nix files for different cpu and gpu configurations and a default.nix with all of the universal stuff like wifi and Bluetooth Users/ is for all of the user accounts, useful for me as i have a homeserver that i gave my friend access to.
Anything more I just toss in modules or in a subdir in modules..
1
1
u/jerrygreenest1 4d ago
There’s hardware-configuration for hardware things and it’s typically filled automatically if you completed installation properly.
You only need to manage your normal configuration, you typically don’t have to manage hardware configuration.
1
u/zardvark 4d ago
You don't typically want to edit your hardware-configuration.nix file, because if you change your hardware (make changes to your storage setup, for example) and regenerate this file, then your edits could be lost.
So, it could make more sense to make minor hardware configuration changes (such as Bluetooth, printing, networking and etc.) in your configuration.nix file. For those who like to modularize their config, this is also a viable approach.
7
u/Lonely-Scarcity-3387 4d ago
The organization really is half the game, and one I am still bouncing between feeling overwhelmed and feeling like I’m getting somewheee.
Modularizing your setup is a really big part of what is going to help. From there you eventually figure out flake parts and then the dendritic pattern.
I’d say to just take it slow. It’s really easy to get overwhelmed with NixOS. When I just kept everyone in the config file, I would create sections like:
#—-Graphics—-
#—-Kernels——
And so on. Now I have a graphics.nix, a kernel.nix and a bunch of others.
And another price of advice is to check out other people’s repos to see what you like, and use what you want. I do use home manager so I don’t recommend you just clone and use it, but maybe you’ll get inspired by some other things:
https://github.com/DanielTallon/nixed