Flakes and files that should not be commited

tl;dr at the bottom; but I maybe explanation on my config are necessary to help me so here it id:

I think a common use case when having a config for multiple system is to make the #home and #host param of the flake command generic; and in the flake read those infos from somewhere else. Personally, I have a .username and .hostname in my dotfiles. Those are meant to hold the current system infos.

In my flake.nix file, I have something like this to build the correct system/home based on those:

{
  outputs = inputs @ {    nixpkgs,    nixpkgs-unstable,    ...  }: let
    baseSystem = lib.removeSuffix "\n" (builtins.readFile ./.system);
    baseHostname = lib.removeSuffix "\n" (builtins.readFile ./.hostname);
    baseUsername = lib.removeSuffix "\n" (builtins.readFile ./.username);

    system = baseSystem;
    pkgs = nixpkgs.legacyPackages.${system};
    pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
  in {
    nixosConfigurations = {
      system = lib.nixosSystem {
        system = baseSystem;
        modules = [
          hosts/.common # Host's common modules
          hosts/${baseHostname} # This host's special config
        ];
      };
    };

    homeConfigurations = {
      user = home-manager.lib.homeManagerConfiguration {
        modules = [
          homes/.common # User's common modules
          homes/${baseUsername} # This user's special config
        ];
      };
    };
  };
}

With that, I can rebuild with the following same command on any of my computers:
nixos-rebuild switch --flake ~/.dotfiles#system or home-manager switch --flake ~/.dotfiles#user

The advantages of this is that when I reinstall my computer or a new one, i just have to fill those three files and I never have to care about passing the correct # param to --flake. It migh look overcomplicated; but all my installation and maintenance is handled via one bash script and in combination with that “complicated” setup actually make the whole maintenance really trivial, with short commands and with git fully integrated into my rebuild system.

TL;DR: Into my problem; those three files are dependent on each computer and should not be pushed to git, but still detected by flake. I know this is a recurring problem, and I’m not sure if there is a direct solution to it.

I’ve tried committing the default empty files and then telling git to ignore them even tho already committed with git update-index --assume-unchanged .username .hostname without success. And unless flake provide a way to configure files to be used even if untracked, I don’t see how I can fix that.

But maybe someone will find a clever solution to my situation, for the time being all I can think of is put those files outside the dotfiles folder and then calling the rebuild with --impure. But I don’t find that solution super elegant.

For reference my setup is heavily inspired by other setup, this mecanism is specificaly inspired by LibrePhoenix’s own flake. His install dependents stuff is in the flake instead of dedicated files so he has to edit them before each install; I try to get rid of that process at least when reinstalling a known host and home.

You can specify flake as a path, not as a git repo. I do

doas nixos-rebuild switch --flake path:/home/matklad/config

to allow dependency on gitignored files.

2 Likes