Skip to content

Commit 23b8cce

Browse files
committed
nix-flakes.nix, nix-channel.nix: split out from nix.nix
Decouple the modules, enable ones to import/disable them separately, and to align with the upstream Nixpkgs/NixOS module system.
1 parent cee3b09 commit 23b8cce

File tree

3 files changed

+143
-74
lines changed

3 files changed

+143
-74
lines changed

modules/environment/nix-channel.nix

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
2+
3+
# Based on
4+
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix-channel.nix
5+
# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors,
6+
# licensed under MIT License as well)
7+
8+
{ config, lib, pkgs, nixpkgs, ... }:
9+
10+
with lib;
11+
12+
let
13+
cfg = config.nix;
14+
renameNixOpt = old: new:
15+
(mkRenamedOptionModule [ "nix" old ] [ "nix" new ]);
16+
in
17+
18+
{
19+
###### interface
20+
21+
options = {
22+
23+
nix = {
24+
nixPath = mkOption {
25+
type = types.listOf types.str;
26+
default = [ ];
27+
description = ''
28+
The default Nix expression search path, used by the Nix
29+
evaluator to look up paths enclosed in angle brackets
30+
(e.g. <literal>&lt;nixpkgs&gt;</literal>).
31+
'';
32+
};
33+
};
34+
35+
};
36+
37+
38+
###### implementation
39+
40+
config = {
41+
environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath;
42+
};
43+
44+
}

modules/environment/nix-flakes.nix

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
2+
3+
# Based on
4+
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix-flakes.nix
5+
# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors,
6+
# licensed under MIT License as well)
7+
8+
{ config, lib, pkgs, ... }:
9+
10+
with lib;
11+
12+
let
13+
cfg = config.nix;
14+
in
15+
16+
{
17+
###### interface
18+
19+
options = {
20+
nix = {
21+
registry = mkOption {
22+
type = types.attrsOf (types.submodule (
23+
let
24+
referenceAttrs = with types; attrsOf (oneOf [
25+
str
26+
int
27+
bool
28+
package
29+
]);
30+
in
31+
{ config, name, ... }:
32+
{
33+
options = {
34+
from = mkOption {
35+
type = referenceAttrs;
36+
example = { type = "indirect"; id = "nixpkgs"; };
37+
description = "The flake reference to be rewritten.";
38+
};
39+
to = mkOption {
40+
type = referenceAttrs;
41+
example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; };
42+
description = "The flake reference <option>from</option> is rewritten to.";
43+
};
44+
flake = mkOption {
45+
type = types.nullOr types.attrs;
46+
default = null;
47+
example = literalExpression "nixpkgs";
48+
description = ''
49+
The flake input <option>from</option> is rewritten to.
50+
'';
51+
};
52+
exact = mkOption {
53+
type = types.bool;
54+
default = true;
55+
description = ''
56+
Whether the <option>from</option> reference needs to match exactly. If set,
57+
a <option>from</option> reference like <literal>nixpkgs</literal> does not
58+
match with a reference like <literal>nixpkgs/nixos-20.03</literal>.
59+
'';
60+
};
61+
};
62+
config = {
63+
from = mkDefault { type = "indirect"; id = name; };
64+
to = mkIf (config.flake != null) (mkDefault
65+
{
66+
type = "path";
67+
path = config.flake.outPath;
68+
} // filterAttrs
69+
(n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash")
70+
config.flake);
71+
};
72+
}
73+
));
74+
default = { };
75+
description = "A system-wide flake registry.";
76+
};
77+
};
78+
};
79+
80+
81+
###### implementation
82+
83+
config = {
84+
environment.etc = {
85+
"nix/registry.json".text = builtins.toJSON {
86+
version = 2;
87+
flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry;
88+
};
89+
};
90+
};
91+
92+
}

modules/environment/nix.nix

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
22

33
# Based on
4-
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/nix-daemon.nix
4+
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/system/nix-daemon.nix
5+
# and
6+
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/nix.nix
57
# (Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors,
68
# licensed under MIT License as well)
79

@@ -27,6 +29,9 @@ in
2729
options = {
2830

2931
nix = {
32+
33+
## From nix-daemon.nix
34+
3035
enable = mkOption {
3136
type = types.bool;
3237
default = true;
@@ -46,72 +51,7 @@ in
4651
'';
4752
};
4853

49-
nixPath = mkOption {
50-
type = types.listOf types.str;
51-
default = [ ];
52-
description = ''
53-
The default Nix expression search path, used by the Nix
54-
evaluator to look up paths enclosed in angle brackets
55-
(e.g. <literal>&lt;nixpkgs&gt;</literal>).
56-
'';
57-
};
58-
59-
registry = mkOption {
60-
type = types.attrsOf (types.submodule (
61-
let
62-
referenceAttrs = with types; attrsOf (oneOf [
63-
str
64-
int
65-
bool
66-
package
67-
]);
68-
in
69-
{ config, name, ... }:
70-
{
71-
options = {
72-
from = mkOption {
73-
type = referenceAttrs;
74-
example = { type = "indirect"; id = "nixpkgs"; };
75-
description = "The flake reference to be rewritten.";
76-
};
77-
to = mkOption {
78-
type = referenceAttrs;
79-
example = { type = "github"; owner = "my-org"; repo = "my-nixpkgs"; };
80-
description = "The flake reference <option>from</option> is rewritten to.";
81-
};
82-
flake = mkOption {
83-
type = types.nullOr types.attrs;
84-
default = null;
85-
example = literalExpression "nixpkgs";
86-
description = ''
87-
The flake input <option>from</option> is rewritten to.
88-
'';
89-
};
90-
exact = mkOption {
91-
type = types.bool;
92-
default = true;
93-
description = ''
94-
Whether the <option>from</option> reference needs to match exactly. If set,
95-
a <option>from</option> reference like <literal>nixpkgs</literal> does not
96-
match with a reference like <literal>nixpkgs/nixos-20.03</literal>.
97-
'';
98-
};
99-
};
100-
config = {
101-
from = mkDefault { type = "indirect"; id = name; };
102-
to = mkIf (config.flake != null) (mkDefault
103-
{
104-
type = "path";
105-
path = config.flake.outPath;
106-
} // filterAttrs
107-
(n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash")
108-
config.flake);
109-
};
110-
}
111-
));
112-
default = { };
113-
description = "A system-wide flake registry.";
114-
};
54+
## From nix.nix
11555

11656
substituters = mkOption {
11757
type = types.listOf types.str;
@@ -152,11 +92,6 @@ in
15292
trusted-public-keys = ${concatStringsSep " " cfg.trustedPublicKeys}
15393
${cfg.extraOptions}
15494
'';
155-
156-
"nix/registry.json".text = builtins.toJSON {
157-
version = 2;
158-
flakes = mapAttrsToList (_n: v: { inherit (v) from to exact; }) cfg.registry;
159-
};
16095
};
16196

16297
nix = {
@@ -170,8 +105,6 @@ in
170105
"nix-on-droid.cachix.org-1:56snoMJTXmDRC1Ei24CmKoUqvHJ9XCp+nidK7qkMQrU="
171106
];
172107
};
173-
174-
environment.sessionVariables.NIX_PATH = concatStringsSep ":" cfg.nixPath;
175108
};
176109

177110
}

0 commit comments

Comments
 (0)