-
Notifications
You must be signed in to change notification settings - Fork 40
/
base.nix
177 lines (172 loc) · 7.02 KB
/
base.nix
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
{ pkgs, config, lib, ... }:
with lib;
{
options = {
boot.isContainer = mkOption {
type = types.bool;
default = false;
};
hardware.firmware = mkOption {
type = types.listOf types.package;
default = [];
apply = list: pkgs.buildEnv {
name = "firmware";
paths = list;
pathsToLink = [ "/lib/firmware" ];
ignoreCollisions = true;
};
};
not-os.nix = mkOption {
type = types.bool;
description = "enable nix-daemon and a writeable store";
};
not-os.rngd = mkOption {
type = types.bool;
description = "enable rngd";
default = false;
};
not-os.simpleStaticIp = mkOption {
type = types.bool;
default = false;
description = "set a static ip of 10.0.2.15";
};
networking.timeServers = mkOption {
default = [
"0.nixos.pool.ntp.org"
"1.nixos.pool.ntp.org"
"2.nixos.pool.ntp.org"
"3.nixos.pool.ntp.org"
];
type = types.listOf types.str;
description = ''
The set of NTP servers from which to synchronise.
'';
};
system.systemBuilderArgs = mkOption {
type = types.attrsOf types.unspecified;
internal = true;
default = {};
description = lib.mdDoc ''
`lib.mkDerivation` attributes that will be passed to the top level system builder.
'';
};
system.systemBuilderCommands = mkOption {
type = types.lines;
internal = true;
default = "";
description = ''
This code will be added to the builder creating the system store path.
'';
};
};
config = {
environment.systemPackages = lib.optional config.not-os.nix pkgs.nix;
nixpkgs.config = {
packageOverrides = self: {
utillinux = self.utillinux.override { systemd = null; systemdSupport = false; };
dhcpcd = self.dhcpcd.override { udev = null; };
linux_rpixxx = self.linux_rpi.override {
extraConfig = ''
DEBUG_LL y
EARLY_PRINTK y
DEBUG_BCM2708_UART0 y
ARM_APPENDED_DTB n
ARM_ATAG_DTB_COMPAT n
ARCH_BCM2709 y
BCM2708_GPIO y
BCM2708_NOL2CACHE y
BCM2708_SPIDEV y
'';
};
};
};
environment.etc = {
"nix/nix.conf".source = pkgs.runCommand "nix.conf" {} ''
extraPaths=$(for i in $(cat ${pkgs.writeReferencesToFile pkgs.runtimeShell}); do if test -d $i; then echo $i; fi; done)
cat > $out << EOF
build-use-sandbox = true
build-users-group = nixbld
build-sandbox-paths = /bin/sh=${pkgs.runtimeShell} $(echo $extraPaths)
build-max-jobs = 1
build-cores = 4
EOF
'';
bashrc.text = "export PATH=/run/current-system/sw/bin";
profile.text = "export PATH=/run/current-system/sw/bin";
"resolv.conf".text = "nameserver 10.0.2.3";
passwd.text = ''
root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
sshd:x:498:65534:SSH privilege separation user:/var/empty:/run/current-system/sw/bin/nologin
toxvpn:x:1010:65534::/var/lib/toxvpn:/run/current-system/sw/bin/nologin
nixbld1:x:30001:30000:Nix build user 1:/var/empty:/run/current-system/sw/bin/nologin
nixbld2:x:30002:30000:Nix build user 2:/var/empty:/run/current-system/sw/bin/nologin
nixbld3:x:30003:30000:Nix build user 3:/var/empty:/run/current-system/sw/bin/nologin
nixbld4:x:30004:30000:Nix build user 4:/var/empty:/run/current-system/sw/bin/nologin
nixbld5:x:30005:30000:Nix build user 5:/var/empty:/run/current-system/sw/bin/nologin
nixbld6:x:30006:30000:Nix build user 6:/var/empty:/run/current-system/sw/bin/nologin
nixbld7:x:30007:30000:Nix build user 7:/var/empty:/run/current-system/sw/bin/nologin
nixbld8:x:30008:30000:Nix build user 8:/var/empty:/run/current-system/sw/bin/nologin
nixbld9:x:30009:30000:Nix build user 9:/var/empty:/run/current-system/sw/bin/nologin
nixbld10:x:30010:30000:Nix build user 10:/var/empty:/run/current-system/sw/bin/nologin
'';
"nsswitch.conf".text = ''
hosts: files dns myhostname mymachines
networks: files dns
'';
"services".source = pkgs.iana-etc + "/etc/services";
group.text = ''
root:x:0:
nixbld:x:30000:nixbld1,nixbld10,nixbld2,nixbld3,nixbld4,nixbld5,nixbld6,nixbld7,nixbld8,nixbld9
'';
"ssh/ssh_host_rsa_key.pub".source = ./ssh/ssh_host_rsa_key.pub;
"ssh/ssh_host_rsa_key" = { mode = "0600"; source = ./ssh/ssh_host_rsa_key; };
"ssh/ssh_host_ed25519_key.pub".source = ./ssh/ssh_host_ed25519_key.pub;
"ssh/ssh_host_ed25519_key" = { mode = "0600"; source = ./ssh/ssh_host_ed25519_key; };
};
boot.kernelParams = [ "systemConfig=${config.system.build.toplevel}" ];
boot.kernelPackages = lib.mkDefault (if pkgs.system == "armv7l-linux" then pkgs.linuxPackages_rpi1 else pkgs.linuxPackages);
system.build.earlyMountScript = pkgs.writeScript "dummy" ''
'';
system.build.runvm = pkgs.writeScript "runner" ''
#!${pkgs.stdenv.shell}
exec ${pkgs.qemu_kvm}/bin/qemu-kvm -name not-os -m 512 \
-drive index=0,id=drive1,file=${config.system.build.squashfs},readonly,media=cdrom,format=raw,if=virtio \
-kernel ${config.system.build.kernel}/bzImage -initrd ${config.system.build.initialRamdisk}/initrd -nographic \
-append "console=ttyS0 ${toString config.boot.kernelParams} quiet panic=-1" -no-reboot \
-net nic,model=virtio \
-net user,net=10.0.2.0/24,host=10.0.2.2,dns=10.0.2.3,hostfwd=tcp::2222-:22 \
-device virtio-rng-pci
'';
system.build.dist = pkgs.runCommand "not-os-dist" {} ''
mkdir $out
cp ${config.system.build.squashfs} $out/root.squashfs
cp ${config.system.build.kernel}/*Image $out/kernel
cp ${config.system.build.initialRamdisk}/initrd $out/initrd
echo "${builtins.unsafeDiscardStringContext (toString config.boot.kernelParams)}" > $out/command-line
'';
system.activationScripts.users = ''
# dummy to make setup-etc happy
'';
system.activationScripts.groups = ''
# dummy to make setup-etc happy
'';
system.activationScripts.etc = stringAfter [ "users" "groups" ] config.system.build.etcActivationCommands;
# nix-build -A system.build.toplevel && du -h $(nix-store -qR result) --max=0 -BM|sort -n
system.build.toplevel = pkgs.runCommand "not-os" {
activationScript = config.system.activationScripts.script;
} ''
mkdir $out
cp ${config.system.build.bootStage2} $out/init
substituteInPlace $out/init --subst-var-by systemConfig $out
ln -s ${config.system.path} $out/sw
echo "$activationScript" > $out/activate
substituteInPlace $out/activate --subst-var out
chmod u+x $out/activate
unset activationScript
'';
# nix-build -A squashfs && ls -lLh result
system.build.squashfs = pkgs.callPackage (pkgs.path + "/nixos/lib/make-squashfs.nix") {
storeContents = [ config.system.build.toplevel ];
};
};
}