Nix Flakes configuration for MacOS, NixOS and WSL
1{ inputs, lib, config, ... }:
2let
3 mkNixos = { hostname, system, modules, stateVersion }:
4 inputs.nixpkgs.lib.nixosSystem {
5 inherit system;
6 modules =
7 modules ++ [
8 inputs.self.nixosModules.unfree-packages
9 inputs.self.nixosModules.garbageCollector
10 ({ ... }: {
11 networking.hostName = hostname;
12 nix.settings.experimental-features = [ "nix-command" "flakes" ];
13
14 # This value determines the NixOS release from which the default
15 # settings for stateful data, like file locations and database versions
16 # on your system were taken. It‘s perfectly fine and recommended to leave
17 # this value at the release version of the first install of this system.
18 # Before changing this value read the documentation for this option
19 # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
20 system.stateVersion = stateVersion; # Did you read the comment?
21 })];
22 specialArgs = { inherit inputs; };
23 };
24
25 mkDarwin = { hostname, system, modules, stateVersion }:
26 inputs.darwin.lib.darwinSystem {
27 inherit system;
28 inputs = { nixpkgs = inputs.nixpkgs-darwin; };
29 modules = [
30 inputs.self.darwinModules.unfree-packages
31 inputs.self.darwinModules.garbageCollector
32 ({ ... }: {
33 nix.enable = true;
34 networking.hostName = hostname;
35 networking.computerName = hostname;
36 nix.settings.experimental-features = [ "nix-command" "flakes" ];
37
38 # Used for backwards compatibility, please read the changelog before changing.
39 # $ darwin-rebuild changelog
40 system.stateVersion = stateVersion;
41 })] ++ modules;
42 specialArgs = { inherit inputs; };
43 };
44
45 isDarwin = system:
46 let
47 parsed = lib.systems.elaborate { system = system; };
48 in
49 parsed.isDarwin;
50in
51{
52 options.hosts = lib.mkOption {
53 type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
54 options = {
55 hostname = lib.mkOption {
56 type = lib.types.str;
57 default = name;
58 description = "Hostname for this system";
59 };
60
61 system = lib.mkOption {
62 type = lib.types.str;
63 default = "x86_64-linux";
64 description = "System architecture";
65 };
66
67 modules = lib.mkOption {
68 type = lib.types.listOf lib.types.unspecified;
69 default = [];
70 description = "List of modules to include";
71 };
72
73 stateVersion = lib.mkOption {
74 type = lib.types.oneOf [ lib.types.str lib.types.int ];
75 default = "24.05";
76 description = "State version for the system";
77 };
78 };
79 }));
80 default = {};
81 description = "Host definitions";
82 };
83
84 # NixOS configurations
85 config.flake.nixosConfigurations = lib.mapAttrs
86 (name: cfg: mkNixos {
87 inherit (cfg) hostname system modules stateVersion;
88 })
89 (lib.filterAttrs (name: cfg: !isDarwin cfg.system) config.hosts);
90
91 # Darwin configurations
92 config.flake.darwinConfigurations = lib.mapAttrs
93 (name: cfg: mkDarwin {
94 inherit (cfg) hostname system modules stateVersion;
95 })
96 (lib.filterAttrs (name: cfg: isDarwin cfg.system) config.hosts);
97}