srid / nixos-unified

Unify NixOS + nix-darwin + home-manager configuration in a single flake, while providing a consistent interface at DX and UX level.
https://nixos-unified.org
MIT License
218 stars 21 forks source link

Is there a way to specify different nixpkgs for different hosts? #51

Open akavel opened 6 months ago

akavel commented 6 months ago

Currently, I am trying to track nixpkgs versions separately for different hosts. My old flake.nix had the following:

{
  inputs = {
    # To update nixpkgs (and thus NixOS), pick the nixos-20.09 rev from
    # https://status.nixos.org/
    # (Or, alternatively, nixos-unstable rev.)
    # 
    # 22.05 @21.11.2022
    nixpkgs.url = "github:nixos/nixpkgs/cf63ade6f74bbc9d2a017290f1b2e33e8fbfa70a";
    # 23.11 @07.05.2024
    nixpkgs2.url = "github:nixos/nixpkgs/27c13997bf450a01219899f5a83bd6ffbfc70d3c";
    nixos-wsl.url = "github:nix-community/NixOS-WSL/main";
  };

  outputs = { self, nixpkgs, nixpkgs2, nixos-wsl }: {
    # choice will be done "automatically" based on hostname
    nixosConfigurations.myhost1 = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [ ./myhost1.nix ];
    };
    nixosConfigurations.myhost2 = nixpkgs2.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        nixos-wsl.nixosModules.default
        ./myhost2.nix
      ];
    };
  };
}

I'm trying to port this config to nixos-flake, to let me unify it with my home-manager configs. However, I noticed that nixos-flake seems to be hardcoded to use inputs.nixpkgs, and inputs are passed via flake-parts.lib.mkFlake { inherit inputs; }, so I don't see a clear way to use different nixpkgs between different nixosConfigurations inside nixos-flake. Do you have any suggestions how I could try doing this? I am ok to try modifying nixos-flake code, but I'd be grateful for suggestions how to try approaching that in such case; currently flake-parts is still quite confusing to me; and also you know nixos-flake better, so may have better ideas what approach could be better suited to it.

srid commented 6 months ago

@akavel Just wanted to leave a note saying that I'd be happy to satisfy this use case. In fact, I'd need it for myself as well. Just not sure what the best approach is. There is --override-input, but now we are externalizing the nixpkgs dependency outside of flake.nix ...

akavel commented 5 months ago

@srid I'm afraid both flake-parts and nixos-flake keep being super confusing to me, and it looks like I still have hard time wrapping my head around either 😓 so I only wish I could suggest some solution... The only thing I can try to say is somewhat naively from user perspective; as such, looking e.g. at https://github.com/srid/nixos-config/blob/f1e560296d62c192c6a1a1dd1ca54c237a2cbf96/flake.nix, personally the first idea that comes to my mind, is I'd love if I could e.g. replace line 97-98:

  self.nixos-flake.lib.mkLinuxSystem
    ./systems/ax41.nix;

with something like:

  self.nixos-flake.lib.mkLinuxSystemWith { nixpkgs = inputs.nixpkgs2; }
    ./systems/ax41.nix;
srid commented 5 months ago

One approach we could adopt is to totally separate out the flakes. Each host gets its own flake.nix, but there's also a common flake (containing modules used by all hosts for example) that gets shared by these flakes. The whole thing can be in a mono repo, so we would need --override-input.

srid commented 5 months ago

One approach we could adopt is to totally separate out the flakes. Each host gets its own flake.nix, but there's also a common flake (containing modules used by all hosts for example) that gets shared by these flakes. The whole thing can be in a mono repo, so we would need --override-input.

Yup, I am able to get this working! Part of that work involves #54

I will document it all at the end.