-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuilders.nix
More file actions
131 lines (112 loc) · 4.49 KB
/
Copy pathbuilders.nix
File metadata and controls
131 lines (112 loc) · 4.49 KB
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
{
lib,
inputs,
withSystem,
...
}:
let
inherit (inputs) self;
inherit (lib.lists) optionals singleton concatLists;
inherit (lib.attrsets) recursiveUpdate optionalAttrs;
inherit (lib.modules) mkMerge mkDefault evalModules;
inherit (lib.hardware) ldTernary;
# mkSystem is a function that uses withSystem to give us inputs' and self'
# it also assumes the the system type either nixos or darwin and uses the appropriate
mkSystem =
{
host,
system,
modules,
...
}@args:
withSystem system (
{ self', inputs', ... }:
let
pkgs = inputs.nixpkgs.legacyPackages.${system};
# this is used to determine the target system and modules that are going to be needed
# for this specific system
target = ldTernary pkgs "nixos" "darwin";
eval = evalModules {
# we use recursiveUpdate such that users can "override" the specialArgs
specialArgs = recursiveUpdate {
# create the modulesPath based on the system, we need
modulesPath = ldTernary pkgs "${inputs.nixpkgs}/nixos/modules" "${inputs.darwin}/modules";
# laying it out this way is completely arbitrary, however it looks nice i guess
inherit lib;
inherit self self';
inherit inputs inputs';
} (args.specialArgs or { });
# A nominal type for modules. When set and non-null, this adds a check to
# make sure that only compatible modules are imported.
class = target;
modules = concatLists [
# depending on the base operating system we can only use some options therefore these
# options means that we can limit these options to only those given operating systems
[ "${self}/modules/${target}" ]
# configurations based on that are imported based hostname
[ "${self}/systems/${args.host}" ]
# we need to import the module list for our system
# this is either the nixos modules list provided by nixpkgs
# or the darwin modules list provided by nix darwin
(import (
ldTernary pkgs "${inputs.nixpkgs}/nixos/modules/module-list.nix"
"${inputs.darwin}/modules/module-list.nix"
))
(singleton {
networking.hostName = args.host;
# you can also do this as system = args.system; however for evalModules
# this will not work, so we do this instead
nixpkgs.hostPlatform = mkDefault args.system;
})
# if we are on darwin we need to import the nixpkgs source, its used in some
# modules, if this is not set then you will get an error
(optionals pkgs.stdenv.isDarwin (singleton {
nixpkgs.source = mkDefault inputs.nixpkgs;
}))
(args.modules or [ ])
];
};
in
# we broke don't just call evalModules here because we need to be able to
# append system to the final evaluated result since nix darwin uses this to switch
# the configuration on and off
eval // optionalAttrs pkgs.stdenv.isDarwin { system = eval.config.system.build.toplevel; }
);
# mkIso is a helper function that wraps mkSystem to create an iso
# DO NOT use mkSystem here as it is overkill for isos, furthermore we cannot use darwinSystem here
mkIso =
{
host,
system,
modules,
...
}@args:
lib.nixosSystem {
specialArgs = recursiveUpdate { inherit lib self inputs; } (args.specialArgs or { });
modules = concatLists [
# get an installer profile from nixpkgs to base the Isos off of
[ "${inputs.nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel.nix" ]
# import our custom modules for the iso
[ "${self}/modules/iso" ]
# set the hostname for the iso
(singleton {
networking.hostName = args.host;
nixpkgs.hostPlatform = mkDefault args.system;
})
# load any extra arguments the user has supplied
(args.modules or [ ])
];
};
# mkSystems is a wrapper for mkNixSystem to create a list of systems
mkSystems = systems: mkMerge (map (system: { ${system.host} = mkSystem system; }) systems);
# mkIsos likewise to mkSystems is a wrapper for mkIso to create a list of isos
mkIsos = isos: mkMerge (map (iso: { ${iso.host} = mkIso iso; }) isos);
in
{
inherit
mkIso
mkIsos
mkSystem
mkSystems
;
}