summaryrefslogtreecommitdiff
path: root/modules/flake/configs.nix
blob: 290255f2e0eaf9428ad4f645883e0378f24983f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
{
  config,
  lib,
  inputs,
  withSystem,
  ...
}:
let
  cfg = config.configurations;

  applySpecialArgs = lib.recursiveUpdate { inherit inputs; };
  applyModules = lib.concat (
    lib.toList (
      { pkgs, ... }:
      {
        _module.args.inputs' = withSystem pkgs.stdenv.hostPlatform.system ({ inputs', ... }: inputs');
      }
    )
  );

  nixosSystem =
    {
      nixpkgs,
      modules,
      specialArgs,
      ...
    }@args:
    nixpkgs.lib.nixosSystem (
      lib.removeAttrs args [ "nixpkgs" ]
      // {
        modules = applyModules modules;
        specialArgs = applySpecialArgs specialArgs;
      }
    );

  darwinSystem =
    {
      nix-darwin,
      modules,
      specialArgs,
      ...
    }@args:
    nix-darwin.lib.darwinSystem (
      lib.removeAttrs args [ "nix-darwin" ]
      // {
        modules = applyModules modules;
        specialArgs = applySpecialArgs specialArgs;
      }
    );

  homeManagerConfiguration =
    {
      home-manager,
      modules,
      extraSpecialArgs,
      ...
    }@args:
    home-manager.lib.homeManagerConfiguration (
      lib.removeAttrs args [ "home-manager" ]
      // {
        modules = applyModules modules;
        extraSpecialArgs = applySpecialArgs extraSpecialArgs;
      }
    );

  modulesType = lib.types.listOf lib.types.deferredModule;

  specialArgsOption = lib.mkOption {
    type = lib.types.lazyAttrsOf lib.types.raw;
    default = { };
    description = "Extra arguments to pass to all modules, that are available in `imports` but can not be extended or overridden by the `modules`.";
    example = lib.literalExpression ''
      { foo = "bar"; }
    '';
  };

  legacyPackagesType = lib.types.lazyAttrsOf lib.types.raw;
  flakeType = legacyPackagesType;

  freeformType = lib.types.attrsOf lib.types.any;

  nixosConfigurationSubmodule = {
    inherit freeformType;

    options = {
      nixpkgs = lib.mkOption {
        type = flakeType;
        default = inputs.nixpkgs or (throw "`nixpkgs` must be defined");
        defaultText = "inputs.nixpkgs";
        description = "A nixpkgs input.";
        example = lib.literalExpression "inputs.nixpkgs-stable";
      };

      modules = lib.mkOption {
        type = modulesType;
        default = [ ];
        description = "The NixOS modules to include in the system configuration.";
        example = lib.literalExpression ''
          [ ./configuration.nix ]
        '';
      };

      specialArgs = specialArgsOption;
      modulesLocation = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        description = "A default location for modules that aren't passed by path, used for error messages.";
      };
    };
  };

  darwinConfigurationSubmodule = {
    inherit freeformType;

    options = {
      nix-darwin = lib.mkOption {
        type = flakeType;
        default = inputs.nix-darwin or inputs.darwin or (throw "`nix-darwin` must be defined");
        defaultText = "inputs.nix-darwin or inputs.darwin";
        description = "A nix-darwin input.";
        example = lib.literalExpression "inputs.nix-darwin";
      };

      modules = lib.mkOption {
        type = modulesType;
        default = [ ];
        description = "The Darwin modules to include in the system configuration.";
        example = lib.literalExpression ''
          [ ./configuration.nix ]
        '';
      };

      specialArgs = specialArgsOption;
    };
  };

  homeConfigurationSubmodule = {
    inherit freeformType;

    options = {
      home-manager = lib.mkOption {
        type = flakeType;
        default = inputs.home-manager or (throw "`home-manager` must be defined");
        defaultText = "inputs.home-manager";
        description = "A home-manager input.";
        example = lib.literalExpression "inputs.hm";
      };

      pkgs = lib.mkOption {
        type = legacyPackagesType;
        description = "An instance of Nixpkgs to use in the configuration.";
        example = lib.literalExpression ''
          withSystem "x86_64-linux" ({ pkgs, ... }: pkgs)
        '';
      };

      modules = lib.mkOption {
        type = modulesType;
        default = [ ];
        description = "The home-manager modules to include in the configuration.";
        example = lib.literalExpression ''
          [ ./home.nix ]
        '';
      };

      extraSpecialArgs = specialArgsOption;
    };
  };
in
{
  options.configurations = {
    nixos = lib.mkOption {
      type = lib.types.attrsOf (lib.types.submodule nixosConfigurationSubmodule);
      default = { };
      apply = lib.mapAttrs (lib.const nixosSystem);
      description = "A map of configuration names and options for `nixosSystem`.";
      example = lib.literalExpression ''
        {
          my-machine = { modules = [ ./configuration.nix ]; }
        }
      '';
    };

    darwin = lib.mkOption {
      type = lib.types.attrsOf (lib.types.submodule darwinConfigurationSubmodule);
      default = { };
      apply = lib.mapAttrs (lib.const darwinSystem);
      description = "A map of configuration names and options for `darwinSystem`.";
      example = lib.literalExpression ''
        {
          my-mac = { modules = [ ./darwin-configuration.nix ]; }
        }
      '';
    };

    home = lib.mkOption {
      type = lib.types.attrsOf (lib.types.submodule homeConfigurationSubmodule);
      default = { };
      apply = lib.mapAttrs (lib.const homeManagerConfiguration);
      description = "A map of configuration names and options for `homeManagerConfiguration`.";
      example = lib.literalExpression ''
        {
          my-home = { modules = [ ./home.nix ]; }
        }
      '';
    };
  };

  config.flake = {
    nixosConfigurations = cfg.nixos;
    darwinConfigurations = cfg.darwin;
    homeConfigurations = cfg.home;
  };
}