One Flake to Rule them All

Hello fellow nixers,

  1. My main branch works thanks much to @TLATER & @NobbZ, but it’s a bit of a mess with all the files and code. I’m trying to simplify it and move to my testing branch, but I’ve hit a snag and my flake doesn’t run correctly because of my code is incorrect. I’m thinking the way I have the machines and users coded is the issue. Hopefully I am on the right track and this can be fixed with a little editing.

  2. My end goal is to get to the point where I can specify a PC and user(s) to my flake & configuration files so that the appropriate packages will be installed on each machine as each user prefers.

Basically, I would like to be able to install nixos on a PC, download my flake from my git site, and be able to build the appropriate derivation for that PC and user. I have a personal desktop, laptop, a server, and some family & friends’ PCs that I would like to eventually have everything consolidated and managed by this set of files. I am currently copying the configuration.nix and hardware-configuration.nix located at /etc/nixos into the appropriate subfolder for each machine in this setup. I’m reached the limit of knowledge and would appreciate any help as I can’t seem to make any more progress struggling through this on my own.

Here is my ‘testing’ branch of git for reference:

Your help is much appreciated and hopefully some like minds will find this topic and benefit as well!

1 Like

@matthewcroughan got a pretty good template, if not a little advanced.

you may find joy in

or you may not.

Don’t take @matthewcroughan code as gospel, there are 1000’s way to layout things, because nix is a programming language… .and you can do what you want , how you want.

That’s worse and best thing about it Nixel! :-).

Most of what I have is based on colemickens (Cole Mickens) · GitHub which has gotten more advanced over time too. If I were to make a new general layout, I would probably make more use of the NixOS module system and get rid of the profiles/ mixins/ distinction.

1 Like

Freezer? The Mighty Cole Mickens… Nix God!

Legendary!

They exist! I’ve seen them… It’s not an super advanced AI! it was a real flesh and blood human. I wish they had stayed for socials.

Personally I have one big “default” module that configures the things I want all my devices to share, and then I have computer-specific modules for the more specific things. Over time I’ve found keeping things (relatively) simple pays off a lot.

My flake then has an entry point for each actual computer, importing the default and its specific module: dotfiles/flake.nix at 704d3a154eecc732cc3c78b7f23bf0b0341d4d41 · TLATER/dotfiles · GitHub

Since I don’t really prepare this for anyone else, the default module also sets up my home-manager configuration.

If I were to add more than just one user to this, I would probably also create multiple home-manager modules, one for each user, and then instead of importing user config in the default module import the correct user modules for the specific computers in their modules.

I make use of the fact that import <dirname> == import <dirname>/default.nix a lot, a newbie might want to be aware of this when reading through my repository.

Edit: Really need to go through and reshuffle what I consider “device specific” at some point x)

Thanks @nixinator! Looks nice and clean. I started to go through your flake.nix and looked briefly at the t480. Will try to clean up my code based on this and will likely be back after the holidays as everything is crazy busy now.

@matthewcroughan thanks as well! Will be looking at your configs in parallel soon!

I had know idea I was in the presence of legends…

Are you sure it’s not AI? Can you tell if I am or not??

Obligatory Yul Brynner emoji: :cowboy_hat_face:

1 Like

In order to keep this simple and slowly build around what I learn, let’s say I want to rename my hostname, but keep my user the same for my home folder and home-manager, etc.

Going from:

nixosConfigurations = {
  nixos = nixpkgs.lib.nixosSystem {
   ...

to:

nixosConfigurations = {
  newhostname= nixpkgs.lib.nixosSystem {
   ...

Or am I making to much of a jump because of my already existing home folder location?

I’m trying to keep it simple and create one file that can build both systems. If I understand it correctly I would run :
nixos-rebuild switch --flake .#nixos on one PC and nixos-rebuild switch --flake .#newhostname on the other machine to build the appropriate hostname on the appropriate machines. And I could keep my username and home-manger username all the same underneath and each system would build with the global and home-manager packages that I specified.

I’m not quite there though.

That should be correct, assuming both of those import the same home-manager modules.

home-manager should also be smart enough to deal with existing home folders, it will tell you if files exist that it would override, and ask you to move them before it does so.

Okay, thanks. I will go back to my last working flake files and try to rewrite it for just two machines. Thanks for pointing me in the right direction!

It took me a while to learn, clean up my configs (at least a little), and get past some other obligations, but I was able to build a separate PC last night using the same flake file. I pulled it from my git site, ran sudo nixos-rebuild switch --flake .#<hostname> and it turned out great!

Special thanks to @nixinator, @matthewcroughan, and @TLATER for all the examples and help you provided!

I’m going to continue customizing my flake using these examples on my other machines, but am going to mark this chain as the solution as I obtained my main goal for this topic.

2 Likes