|
2 | 2 |
|
3 | 3 | # Based on |
4 | 4 | # https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/nix-daemon.nix |
5 | | -# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors, |
6 | | -# licensed under MIT License as well) |
| 5 | +# (Copyright (c) 2003-2023 Eelco Dolstra and the Nixpkgs/NixOS contributors) |
| 6 | +# and |
| 7 | +# https://github.com/nix-community/home-manager/blob/master/modules/misc/nix.nix |
| 8 | +# (Copyright (c) 2017-2023 Home Manager contributors) |
| 9 | +# both licensed under MIT License as well) |
7 | 10 |
|
8 | 11 | { config, lib, pkgs, ... }: |
9 | 12 |
|
10 | 13 | with lib; |
11 | 14 |
|
12 | 15 | let |
13 | 16 | cfg = config.nix; |
| 17 | + |
14 | 18 | renameNixOpt = old: new: |
15 | 19 | (mkRenamedOptionModule [ "nix" old ] [ "nix" new ]); |
| 20 | + |
| 21 | + isNixAtLeast = versionAtLeast (getVersion cfg.package); |
| 22 | + |
| 23 | + nixConf = |
| 24 | + assert isNixAtLeast "2.2"; |
| 25 | + let |
| 26 | + |
| 27 | + mkValueString = v: |
| 28 | + if v == null then "" |
| 29 | + else if isInt v then toString v |
| 30 | + else if isBool v then boolToString v |
| 31 | + else if isFloat v then floatToString v |
| 32 | + else if isList v then toString v |
| 33 | + else if isDerivation v then toString v |
| 34 | + else if builtins.isPath v then toString v |
| 35 | + else if isString v then v |
| 36 | + else if strings.isConvertibleWithToString v then toString v |
| 37 | + else abort "The nix conf value: ${toPretty {} v} can not be encoded"; |
| 38 | + |
| 39 | + mkKeyValue = k: v: "${escape [ "=" ] k} = ${mkValueString v}"; |
| 40 | + |
| 41 | + mkKeyValuePairs = attrs: concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs); |
| 42 | + |
| 43 | + in |
| 44 | + pkgs.writeTextFile { |
| 45 | + name = "nix.conf"; |
| 46 | + text = '' |
| 47 | + # WARNING: this file is generated from the nix.* options in |
| 48 | + # your NixOS configuration, typically |
| 49 | + # /etc/nixos/configuration.nix. Do not edit it! |
| 50 | + ${mkKeyValuePairs cfg.settings} |
| 51 | + ${cfg.extraOptions} |
| 52 | + ''; |
| 53 | + checkPhase = lib.optionalString cfg.checkConfig ( |
| 54 | + if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then '' |
| 55 | + echo "Ignoring validation for cross-compilation" |
| 56 | + '' |
| 57 | + else '' |
| 58 | + echo "Validating generated nix.conf" |
| 59 | + ln -s $out ./nix.conf |
| 60 | + set -e |
| 61 | + set +o pipefail |
| 62 | + NIX_CONF_DIR=$PWD \ |
| 63 | + ${cfg.package}/bin/nix show-config ${optionalString (isNixAtLeast "2.3pre") "--no-net"} \ |
| 64 | + ${optionalString (isNixAtLeast "2.4pre") "--option experimental-features nix-command"} \ |
| 65 | + |& sed -e 's/^warning:/error:/' \ |
| 66 | + | (! grep '${if cfg.checkAllErrors then "^error:" else "^error: unknown setting"}') |
| 67 | + set -o pipefail |
| 68 | + '' |
| 69 | + ); |
| 70 | + }; |
| 71 | + |
| 72 | + legacyConfMappings = { |
| 73 | + substituters = "substituters"; |
| 74 | + trustedPublicKeys = "trusted-public-keys"; |
| 75 | + }; |
| 76 | + |
| 77 | + semanticConfType = with types; |
| 78 | + let |
| 79 | + confAtom = nullOr |
| 80 | + (oneOf [ |
| 81 | + bool |
| 82 | + int |
| 83 | + float |
| 84 | + str |
| 85 | + path |
| 86 | + package |
| 87 | + ]) // { |
| 88 | + description = "Nix config atom (null, bool, int, float, str, path or package)"; |
| 89 | + }; |
| 90 | + in |
| 91 | + attrsOf (either confAtom (listOf confAtom)); |
| 92 | + |
16 | 93 | in |
17 | 94 |
|
18 | 95 | { |
|
21 | 98 | (renameNixOpt "binaryCaches" "substituters") |
22 | 99 | (renameNixOpt "binaryCachePublicKeys" "trustedPublicKeys") |
23 | 100 | (renameNixOpt "extraConfig" "extraOptions") |
24 | | - ]; |
| 101 | + ] ++ mapAttrsToList (oldConf: newConf: mkRenamedOptionModule [ "nix" oldConf ] [ "nix" "settings" newConf ]) legacyConfMappings; |
25 | 102 |
|
26 | 103 | ###### interface |
27 | 104 |
|
|
104 | 181 | description = "A system-wide flake registry."; |
105 | 182 | }; |
106 | 183 |
|
107 | | - substituters = mkOption { |
108 | | - type = types.listOf types.str; |
109 | | - default = [ ]; |
| 184 | + extraOptions = mkOption { |
| 185 | + type = types.lines; |
| 186 | + default = ""; |
| 187 | + description = "Extra config to be appended to <filename>/etc/nix/nix.conf</filename>."; |
| 188 | + }; |
| 189 | + |
| 190 | + checkConfig = mkOption { |
| 191 | + type = types.bool; |
| 192 | + default = true; |
110 | 193 | description = '' |
111 | | - A list of URLs of substituters. The official NixOS and Nix-on-Droid |
112 | | - substituters are added by default. |
| 194 | + If enabled, checks that Nix can parse the generated nix.conf. |
113 | 195 | ''; |
114 | 196 | }; |
115 | 197 |
|
116 | | - trustedPublicKeys = mkOption { |
117 | | - type = types.listOf types.str; |
118 | | - default = [ ]; |
| 198 | + checkAllErrors = mkOption { |
| 199 | + type = types.bool; |
| 200 | + default = true; |
119 | 201 | description = '' |
120 | | - A list of public keys. When paths are copied from another Nix store (such as a |
121 | | - binary cache), they must be signed with one of these keys. The official NixOS |
122 | | - and Nix-on-Droid public keys are added by default. |
| 202 | + If enabled, checks the nix.conf parsing for any kind of error. When disabled, checks only for unknown settings. |
123 | 203 | ''; |
124 | 204 | }; |
125 | 205 |
|
126 | | - extraOptions = mkOption { |
127 | | - type = types.lines; |
128 | | - default = ""; |
129 | | - description = "Extra config to be appended to <filename>/etc/nix/nix.conf</filename>."; |
| 206 | + settings = mkOption { |
| 207 | + type = types.submodule { |
| 208 | + freeformType = semanticConfType; |
| 209 | + |
| 210 | + options = { |
| 211 | + substituters = mkOption { |
| 212 | + type = types.listOf types.str; |
| 213 | + description = '' |
| 214 | + A list of URLs of substituters. The official NixOS and Nix-on-Droid |
| 215 | + substituters are added by default. |
| 216 | + ''; |
| 217 | + }; |
| 218 | + |
| 219 | + trusted-public-keys = mkOption { |
| 220 | + type = types.listOf types.str; |
| 221 | + description = '' |
| 222 | + A list of public keys. When paths are copied from another Nix store (such as a |
| 223 | + binary cache), they must be signed with one of these keys. The official NixOS |
| 224 | + and Nix-on-Droid public keys are added by default. |
| 225 | + ''; |
| 226 | + }; |
| 227 | + }; |
| 228 | + }; |
| 229 | + default = { }; |
| 230 | + example = literalExpression '' |
| 231 | + { |
| 232 | + experimental-fetures = [ "nix-commnd" "flake" ]; |
| 233 | + } |
| 234 | + ''; |
| 235 | + description = '' |
| 236 | + Configuration for Nix, see |
| 237 | + <link xlink:href="https://nixos.org/manual/nix/stable/#sec-conf-file"/> or |
| 238 | + <citerefentry> |
| 239 | + <refentrytitle>nix.conf</refentrytitle> |
| 240 | + <manvolnum>5</manvolnum> |
| 241 | + </citerefentry> for available options. |
| 242 | + The value declared here will be translated directly to the key-value pairs Nix expects. |
| 243 | + </para> |
| 244 | + <para> |
| 245 | + Nix configurations defined under <option>nix.*</option> will be translated and applied to this |
| 246 | + option. In addition, configuration specified in <option>nix.extraOptions</option> will be appended |
| 247 | + verbatim to the resulting config file. |
| 248 | + ''; |
130 | 249 | }; |
131 | 250 | }; |
132 | 251 |
|
|
138 | 257 | config = mkMerge [ |
139 | 258 | { |
140 | 259 | environment.etc = { |
141 | | - "nix/nix.conf".text = '' |
142 | | - sandbox = false |
143 | | - substituters = ${concatStringsSep " " cfg.substituters} |
144 | | - trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys} |
145 | | - ${cfg.extraOptions} |
146 | | - ''; |
147 | | - |
| 260 | + "nix/nix.conf".source = nixConf; |
148 | 261 | "nix/registry.json".text = builtins.toJSON { |
149 | 262 | version = 2; |
150 | 263 | flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry; |
151 | 264 | }; |
152 | 265 | }; |
153 | 266 |
|
154 | | - nix = { |
| 267 | + nix.settings = { |
| 268 | + sandbox = false; |
155 | 269 | substituters = [ |
156 | 270 | "https://cache.nixos.org" |
157 | 271 | "https://nix-on-droid.cachix.org" |
158 | 272 | ]; |
159 | | - trustedPublicKeys = [ |
| 273 | + trusted-public-keys = [ |
160 | 274 | "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" |
161 | 275 | "nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU=" |
162 | 276 | ]; |
|
0 commit comments