]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/system/dilion.nix
First attempt at making declarative VMs
[perso/Immae/Config/Nix.git] / modules / private / system / dilion.nix
1 { privateFiles }:
2 { config, pkgs, name, lib, ... }:
3 {
4 deployment = {
5 targetUser = "root";
6 targetHost = config.hostEnv.ips.main.ip4;
7 substituteOnDestination = true;
8 };
9 nixpkgs.system = lib.mkOverride 900 "x86_64-linux";
10 boot = {
11 loader = {
12 grub = {
13 version = 2;
14 devices = [ "/dev/sda" "/dev/sdb" "/dev/sdc" "/dev/sdd" ];
15 };
16 timeout = 1;
17 };
18 blacklistedKernelModules = [ "nvidiafb" ];
19 supportedFilesystems = [ "zfs" ];
20 kernelPackages = pkgs.linuxPackages_latest;
21 kernelModules = [ "kvm-intel" ];
22 initrd.availableKernelModules = [ "ahci" "sd_mod" ];
23 initrd.secrets = {
24 "/boot/pass.key" = "/boot/pass.key";
25 };
26 kernel.sysctl."vm.nr_hugepages" = 256; # for xmr-stak
27 # available in nixos-20.09
28 #zfs.requestEncryptionCredentials = [ "zpool/root" ];
29 };
30 nix.maxJobs = 8;
31 powerManagement.cpuFreqGovernor = "powersave";
32 hardware.enableRedistributableFirmware = true;
33
34 myEnv = import "${privateFiles}/environment.nix" // { inherit privateFiles; };
35
36 swapDevices = [ { label = "swap"; } ];
37 fileSystems = {
38 "/" = { fsType = "zfs"; device = "zpool/root"; };
39 "/boot" = { fsType = "ext4"; device = "/dev/disk/by-uuid/fd1c511e-2bc0-49d5-b8bb-95e7e8c8c816"; };
40 "/etc" = { fsType = "zfs"; device = "zpool/root/etc"; };
41 "/home" = { fsType = "zfs"; device = "zpool/root/home"; };
42 "/home/immae" = { fsType = "zfs"; device = "zpool/root/home/immae"; };
43 "/tmp" = { fsType = "zfs"; device = "zpool/root/tmp"; };
44 "/var" = { fsType = "zfs"; device = "zpool/root/var"; };
45 "/data" = { fsType = "ext4"; label = "data"; };
46 "/nix" = { fsType = "ext4"; label = "nix"; };
47 };
48
49 services.udev.extraRules = ''
50 ACTION=="add", SUBSYSTEM=="net", ATTR{address}=="10:bf:48:7f:e6:3b", NAME="eth0"
51 '';
52
53 networking = {
54 hostId = "27c3048d"; # generated with head -c4 /dev/urandom | od -A none -t x4
55 firewall.enable = false;
56 interfaces."eth0".ipv4.addresses =
57 [ { address = config.hostEnv.ips.main.ip4; prefixLength = 27; } ]
58 ++ pkgs.lib.attrsets.mapAttrsToList
59 (n: ips: { address = ips.ip4; prefixLength = 32; })
60 (pkgs.lib.attrsets.filterAttrs (n: v: n != "main") config.hostEnv.ips);
61 interfaces."eth0".ipv6.addresses =
62 [ { address = "2a01:4f8:141:53e7::"; prefixLength = 64; } ]
63 ++ pkgs.lib.flatten (pkgs.lib.attrsets.mapAttrsToList
64 (n: ips: map (ip: { address = ip; prefixLength = (if n == "main" && ip == pkgs.lib.head ips.ip6 then 64 else 128); }) (ips.ip6 or []))
65 config.hostEnv.ips);
66 defaultGateway = { address = "176.9.10.225"; interface = "eth0"; };
67 defaultGateway6 = { address = "fe80::1"; interface = "eth0"; };
68 nameservers = [
69 "213.133.98.98"
70 "213.133.99.99"
71 "213.133.100.100"
72 "2a01:4f8:0:a0a1::add:1010"
73 "2a01:4f8:0:a102::add:9999"
74 "2a01:4f8:0:a111::add:9898"
75 ];
76 };
77
78 myServices.ssh.modules = [ config.myServices.ssh.predefinedModules.regular ];
79 imports = builtins.attrValues (import ../..) ++ [ ./dilion/vms.nix ];
80
81 system.nssModules = [ pkgs.libvirt ];
82 system.nssDatabases.hosts = lib.mkForce [ "files" "libvirt_guest" "mymachines" "dns" "myhostname" ];
83 programs.zsh.enable = true;
84
85 users.users.libvirt = {
86 hashedPassword = "!";
87 shell = pkgs.bashInteractive;
88 isSystemUser = true;
89 group = "libvirtd";
90 packages = [ pkgs.netcat-openbsd ];
91 openssh.authorizedKeys.keyFiles = [
92 "${privateFiles}/buildbot_ssh_key.pub"
93 ];
94 openssh.authorizedKeys.keys = [ config.myEnv.sshd.rootKeys.ismael_flony ];
95 };
96
97 users.users.backup = {
98 hashedPassword = "!";
99 isSystemUser = true;
100 extraGroups = [ "keys" ];
101 shell = pkgs.bashInteractive;
102 openssh.authorizedKeys.keys = let
103 zreplConfig = config.secrets.fullPaths."zrepl/zrepl.yml";
104 in
105 ["command=\"${pkgs.zrepl}/bin/zrepl stdinserver --config ${zreplConfig} eldiron\",restrict ${config.myEnv.zrepl_backup.ssh_key.public}"];
106 };
107 security.sudo.extraRules = pkgs.lib.mkAfter [
108 {
109 commands = [
110 { command = "/home/immae/.nix-profile/root_scripts/*"; options = [ "NOPASSWD" ]; }
111 ];
112 users = [ "immae" ];
113 runAs = "root";
114 }
115 ];
116
117 system.activationScripts.libvirtd_exports = ''
118 install -m 0755 -o root -g root -d /var/lib/caldance
119 '';
120 virtualisation.docker.enable = true;
121 virtualisation.docker.storageDriver = "zfs";
122 virtualisation.libvirtd.enable = true;
123 users.extraUsers.immae.extraGroups = [ "libvirtd" "docker" ];
124 systemd.services.libvirtd.postStart = ''
125 install -m 0770 -g libvirtd -d /var/lib/libvirt/images
126 '';
127 systemd.services.socat-caldance = {
128 description = "Forward ssh port to caldance";
129 wantedBy = [ "multi-user.target" ];
130 after = [ "network.target" ];
131
132 serviceConfig = {
133 ExecStart = "${pkgs.socat}/bin/socat TCP-LISTEN:8022,fork TCP:caldance:22";
134 };
135 };
136
137 time.timeZone = "Europe/Paris";
138 nix = {
139 useSandbox = "relaxed";
140 extraOptions = ''
141 keep-outputs = true
142 keep-derivations = true
143 allow-unsafe-native-code-during-evaluation = true
144 experimental-features = nix-command flakes
145 #Assumed in NUR
146 allow-import-from-derivation = true
147 '';
148 };
149
150 security.pki.certificateFiles = [
151 (pkgs.fetchurl {
152 url = "http://downloads.e.eriomem.net/eriomemca.pem";
153 sha256 = "1ixx4c6j3m26j8dp9a3dkvxc80v1nr5aqgmawwgs06bskasqkvvh";
154 })
155 ];
156
157 # This is equivalent to setting environment.sessionVariables.NIX_PATH
158 nix.nixPath = [
159 "home-manager=${pkgs.sources.home-manager.url}"
160 "nixpkgs=${pkgs.sources.nixpkgs-home-manager.url}"
161 ];
162 nix.binaryCaches = [ "https://hydra.iohk.io" "https://cache.nixos.org" ];
163 nix.binaryCachePublicKeys = [ "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" ];
164
165 myServices.monitoring.enable = true;
166 myServices.certificates.enable = true;
167 security.acme.certs."${name}-immae" = config.myServices.certificates.certConfig // {
168 user = "immae";
169 domain = "dilion.immae.eu";
170 };
171 security.acme.certs."${name}" = {
172 user = config.services.nginx.user;
173 group = config.services.nginx.group;
174 extraDomains = {
175 "dev.immae.eu" = null;
176 "caldance.immae.eu" = null;
177 };
178 };
179 services.nginx = {
180 enable = true;
181 recommendedOptimisation = true;
182 recommendedGzipSettings = true;
183 recommendedProxySettings = true;
184 upstreams = {
185 caldance.servers."caldance:3031" = {};
186 };
187 virtualHosts = {
188 "dev.immae.eu" = {
189 acmeRoot = config.myServices.certificates.webroot;
190 useACMEHost = name;
191 forceSSL = true;
192 root = "/home/immae/www";
193 };
194 "caldance.immae.eu" = {
195 acmeRoot = config.myServices.certificates.webroot;
196 useACMEHost = name;
197 forceSSL = true;
198 locations."/".extraConfig = ''
199 uwsgi_pass caldance;
200 '';
201 locations."/static/".alias = "/var/lib/caldance/caldance/app/www/static/";
202 locations."/media/".alias = "/var/lib/caldance/caldance/media/";
203 extraConfig = ''
204 auth_basic "Authentification requise";
205 auth_basic_user_file ${pkgs.writeText "htpasswd" config.myEnv.websites.caldance.integration.password};
206 '';
207 };
208 };
209 };
210
211 systemd.services.zrepl.serviceConfig.RuntimeDirectory = lib.mkForce "zrepl zrepl/stdinserver";
212 systemd.services.zrepl.serviceConfig.User = "backup";
213 # zfs allow backup create,mount,receive,destroy,rename,snapshot,hold,bookmark,release zpool/backup
214 services.zrepl = {
215 enable = true;
216 config = ''
217 global:
218 control:
219 sockpath: /run/zrepl/control
220 serve:
221 stdinserver:
222 sockdir: /run/zrepl/stdinserver
223 jobs:
224 - type: sink
225 # must not change
226 name: "backup-from-eldiron"
227 root_fs: "zpool/backup"
228 serve:
229 type: stdinserver
230 client_identities:
231 - eldiron
232 '';
233 };
234 # This value determines the NixOS release with which your system is
235 # to be compatible, in order to avoid breaking some software such as
236 # database servers. You should change this only after NixOS release
237 # notes say you should.
238 # https://nixos.org/nixos/manual/release-notes.html
239 system.stateVersion = "20.03"; # Did you read the comment?
240 }
241