Skip to content

Commit 3db1fca

Browse files
committed
modules/nix: use structural settings (Nix RFC 42)
Port structural settings from nixos/nix-daemon. Adjust the description based on those from previous options and the Home Manager nix.settings option.
1 parent 82b4cd6 commit 3db1fca

File tree

6 files changed

+153
-39
lines changed

6 files changed

+153
-39
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
* New options `networking.hosts`, `networking.hostFiles` and
88
`networking.extraHosts` for `/etc/hosts` configuration.
99

10+
* Add option `nix.settings` to support
11+
[structural `settings`](https://github.com/NixOS/rfcs/blob/master/rfcs/0042-config-option.md)
12+
for `nix.conf`.
13+
`nix.substituters` and `nix.trustedPublicKeys` are now aliases of
14+
`nix.settings.substituters` and `nix.settings.trusted-public-keys`,
15+
respectively.
16+
Nix Flakes functionality can now be enabled with
17+
`nix.settings.experimental-features = [ "nix-command" "flakes" ];`
18+
1019
## Release 23.05
1120

1221
### New Options

modules/environment/login/nix-on-droid.nix.default

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
system.stateVersion = "23.05";
3232

3333
# Set up nix for flakes
34-
#nix.extraOptions = ''
35-
# experimental-features = nix-command flakes
36-
#'';
34+
#nix.settings.experimental-features = [ "nix-command" "flakes" ];
3735

3836
# Set your time zone
3937
#time.timeZone = "Europe/Berlin";

modules/environment/nix.nix

Lines changed: 140 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,93 @@
22

33
# Based on
44
# 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)
710

811
{ config, lib, pkgs, ... }:
912

1013
with lib;
1114

1215
let
1316
cfg = config.nix;
17+
1418
renameNixOpt = old: new:
1519
(mkRenamedOptionModule [ "nix" old ] [ "nix" new ]);
20+
21+
isNixAtLeast = versionAtLeast (getVersion cfg.package);
22+
23+
nixConf =
24+
let
25+
26+
mkValueString = v:
27+
if v == null then ""
28+
else if isInt v then toString v
29+
else if isBool v then boolToString v
30+
else if isFloat v then floatToString v
31+
else if isList v then toString v
32+
else if isDerivation v then toString v
33+
else if builtins.isPath v then toString v
34+
else if isString v then v
35+
else if strings.isConvertibleWithToString v then toString v
36+
else abort "The nix conf value: ${toPretty {} v} can not be encoded";
37+
38+
mkKeyValue = k: v: "${escape [ "=" ] k} = ${mkValueString v}";
39+
40+
mkKeyValuePairs = attrs: concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs);
41+
42+
in
43+
pkgs.writeTextFile {
44+
name = "nix.conf";
45+
text = ''
46+
# WARNING: this file is generated from the nix.* options in
47+
# your NixOS configuration, typically
48+
# /etc/nixos/configuration.nix. Do not edit it!
49+
${mkKeyValuePairs cfg.settings}
50+
${cfg.extraOptions}
51+
'';
52+
checkPhase = lib.optionalString cfg.checkConfig (
53+
if pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform then ''
54+
echo "Ignoring validation for cross-compilation"
55+
''
56+
else ''
57+
echo "Validating generated nix.conf"
58+
ln -s $out ./nix.conf
59+
set -e
60+
set +o pipefail
61+
NIX_CONF_DIR=$PWD \
62+
${cfg.package}/bin/nix show-config ${optionalString (isNixAtLeast "2.3pre") "--no-net"} \
63+
${optionalString (isNixAtLeast "2.4pre") "--option experimental-features nix-command"} \
64+
|& sed -e 's/^warning:/error:/' \
65+
| (! grep '${if cfg.checkAllErrors then "^error:" else "^error: unknown setting"}')
66+
set -o pipefail
67+
''
68+
);
69+
};
70+
71+
legacyConfMappings = {
72+
substituters = "substituters";
73+
trustedPublicKeys = "trusted-public-keys";
74+
};
75+
76+
semanticConfType = with types;
77+
let
78+
confAtom = nullOr
79+
(oneOf [
80+
bool
81+
int
82+
float
83+
str
84+
path
85+
package
86+
]) // {
87+
description = "Nix config atom (null, bool, int, float, str, path or package)";
88+
};
89+
in
90+
attrsOf (either confAtom (listOf confAtom));
91+
1692
in
1793

1894
{
@@ -21,7 +97,7 @@ in
2197
(renameNixOpt "binaryCaches" "substituters")
2298
(renameNixOpt "binaryCachePublicKeys" "trustedPublicKeys")
2399
(renameNixOpt "extraConfig" "extraOptions")
24-
];
100+
] ++ mapAttrsToList (oldConf: newConf: mkRenamedOptionModule [ "nix" oldConf ] [ "nix" "settings" newConf ]) legacyConfMappings;
25101

26102
###### interface
27103

@@ -104,29 +180,71 @@ in
104180
description = "A system-wide flake registry.";
105181
};
106182

107-
substituters = mkOption {
108-
type = types.listOf types.str;
109-
default = [ ];
183+
extraOptions = mkOption {
184+
type = types.lines;
185+
default = "";
186+
description = "Extra config to be appended to <filename>/etc/nix/nix.conf</filename>.";
187+
};
188+
189+
checkConfig = mkOption {
190+
type = types.bool;
191+
default = true;
110192
description = ''
111-
A list of URLs of substituters. The official NixOS and Nix-on-Droid
112-
substituters are added by default.
193+
If enabled, checks that Nix can parse the generated nix.conf.
113194
'';
114195
};
115196

116-
trustedPublicKeys = mkOption {
117-
type = types.listOf types.str;
118-
default = [ ];
197+
checkAllErrors = mkOption {
198+
type = types.bool;
199+
default = true;
119200
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.
201+
If enabled, checks the nix.conf parsing for any kind of error. When disabled, checks only for unknown settings.
123202
'';
124203
};
125204

126-
extraOptions = mkOption {
127-
type = types.lines;
128-
default = "";
129-
description = "Extra config to be appended to <filename>/etc/nix/nix.conf</filename>.";
205+
settings = mkOption {
206+
type = types.submodule {
207+
freeformType = semanticConfType;
208+
209+
options = {
210+
substituters = mkOption {
211+
type = types.listOf types.str;
212+
description = ''
213+
A list of URLs of substituters. The official NixOS and Nix-on-Droid
214+
substituters are added by default.
215+
'';
216+
};
217+
218+
trusted-public-keys = mkOption {
219+
type = types.listOf types.str;
220+
description = ''
221+
A list of public keys. When paths are copied from another Nix store (such as a
222+
binary cache), they must be signed with one of these keys. The official NixOS
223+
and Nix-on-Droid public keys are added by default.
224+
'';
225+
};
226+
};
227+
};
228+
default = { };
229+
example = literalExpression ''
230+
{
231+
experimental-fetures = [ "nix-commnd" "flake" ];
232+
}
233+
'';
234+
description = ''
235+
Configuration for Nix, see
236+
<link xlink:href="https://nixos.org/manual/nix/stable/#sec-conf-file"/> or
237+
<citerefentry>
238+
<refentrytitle>nix.conf</refentrytitle>
239+
<manvolnum>5</manvolnum>
240+
</citerefentry> for available options.
241+
The value declared here will be translated directly to the key-value pairs Nix expects.
242+
</para>
243+
<para>
244+
Nix configurations defined under <option>nix.*</option> will be translated and applied to this
245+
option. In addition, configuration specified in <option>nix.extraOptions</option> will be appended
246+
verbatim to the resulting config file.
247+
'';
130248
};
131249
};
132250

@@ -138,25 +256,20 @@ in
138256
config = mkMerge [
139257
{
140258
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-
259+
"nix/nix.conf".source = nixConf;
148260
"nix/registry.json".text = builtins.toJSON {
149261
version = 2;
150262
flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry;
151263
};
152264
};
153265

154-
nix = {
266+
nix.settings = {
267+
sandbox = false;
155268
substituters = [
156269
"https://cache.nixos.org"
157270
"https://nix-on-droid.cachix.org"
158271
];
159-
trustedPublicKeys = [
272+
trusted-public-keys = [
160273
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
161274
"nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU="
162275
];

templates/advanced/nix-on-droid.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
system.stateVersion = "23.05";
3232

3333
# Set up nix for flakes
34-
nix.extraOptions = ''
35-
experimental-features = nix-command flakes
36-
'';
34+
nix.settings.experimental-features = [ "nix-command" "flakes" ];
3735

3836
# Set your time zone
3937
#time.timeZone = "Europe/Berlin";

templates/home-manager/nix-on-droid.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
system.stateVersion = "23.05";
3232

3333
# Set up nix for flakes
34-
nix.extraOptions = ''
35-
experimental-features = nix-command flakes
36-
'';
34+
nix.settings.experimental-features = [ "nix-command" "flakes" ];
3735

3836
# Set your time zone
3937
#time.timeZone = "Europe/Berlin";

templates/minimal/nix-on-droid.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
system.stateVersion = "23.05";
3232

3333
# Set up nix for flakes
34-
nix.extraOptions = ''
35-
experimental-features = nix-command flakes
36-
'';
34+
nix.settings.experimental-features = [ "nix-command" "flakes" ];
3735

3836
# Set your time zone
3937
#time.timeZone = "Europe/Berlin";

0 commit comments

Comments
 (0)