]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
mermet: pleroma
[sourcephile-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.follows = "julm-nix/doom-emacs";
4 home-manager.follows = "julm-nix/home-manager";
5 julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
6 julm-nix.inputs.nixpkgs.follows = "nixpkgs";
7 nix-formatter-pack.follows = "julm-nix/nix-formatter-pack";
8 nixpkgs.url = "github:NixOS/nixpkgs/720e61ed8de116eec48d6baea1d54469b536b985";
9 pre-commit-hooks.follows = "julm-nix/pre-commit-hooks";
10 };
11
12 outputs = inputs:
13 let
14 remoteNixpkgsPatches = import (inputs.julm-nix + "/nixpkgs/patches.nix");
15 localNixpkgsPatches = [
16 #nixpkgs/patches/systemd-coredump.diff
17 ];
18 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
19 nixpkgsPath = originPkgs.applyPatches {
20 name = "nixpkgs-patched";
21 src = inputs.nixpkgs;
22 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
23 postPatch = ''
24 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
25 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
26 sort | sha256sum | cut -c -7)
27 echo "-patch-$patch" >.version-suffix
28 '';
29 };
30 profile = "/nix/var/nix/profiles/system";
31 inherit (inputs.nixpkgs) lib;
32 #nixosSystem = lib.nixosSystem;
33 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
34 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
35 inherit system;
36 #pkgs = inputs.nixpkgs.legacyPackages.${system};
37 pkgs = import nixpkgsPath {
38 inherit system;
39 overlays =
40 import nixpkgs/overlays.nix ++
41 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
42 };
43 });
44 in
45 {
46 # nix -L build .#nixosConfigurations.mermet.config.system.build.toplevel
47 # nix -L build .#nixosConfigurations.mermet.config.boot.kernelPackages.kernel.configfile
48 # nix -L build .#nixosConfigurations.mermet.pkgs.hello
49 # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.rulese
50 nixosConfigurations = lib.genAttrs
51 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
52 (hostName: nixosSystem
53 {
54 system = null;
55 specialArgs = {
56 # Required to avoid infinite recursion
57 # when `inputs` is used in `imports`.
58 inherit inputs;
59 };
60 modules = [
61 nixos/default.nix
62 (inputs.self + "/hosts/${hostName}.nix")
63 {
64 _module.args = {
65 inherit hostName;
66 hosts = inputs.self.nixosConfigurations;
67 host = inputs.self.nixosConfigurations.${hostName}._module.args;
68 info = import ./info.nix;
69 };
70 nixpkgs.overlays =
71 import nixpkgs/overlays.nix ++
72 import (inputs.julm-nix + "/nixpkgs/overlays.nix");
73 #nixpkgs.buildPlatform = "x86_64-linux";
74 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" ];
75 }
76 inputs.nixpkgs.nixosModules.notDetected
77 inputs.home-manager.nixosModules.home-manager
78 {
79 home-manager.useGlobalPkgs = true;
80 home-manager.useUserPackages = true;
81 home-manager.verbose = true;
82 home-manager.backupFileExtension = null;
83 home-manager.extraSpecialArgs = {
84 inherit hostName inputs;
85 };
86 }
87 {
88 programs.ssh.knownHosts = {
89 carotte = {
90 extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
91 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
92 };
93 mermet = {
94 extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
95 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
96 };
97 losurdo = {
98 extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
99 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
100 };
101 };
102 }
103 ];
104 }
105 );
106
107 # nix -L develop or direnv allow
108 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
109 {
110 inherit pkgs inputs system nixpkgsPath;
111 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
112 });
113
114 # nix -L run .#mermet.switch
115 apps = forAllSystems ({ pkgs, system, ... }:
116 with builtins;
117 mapAttrs
118 (hostName: { config, ... }:
119 let
120 inherit (config.system) build;
121 target = "\"\${TARGET:-root@${config.networking.hostName}.wg}\"";
122 scriptApp = scriptName: ps: script: {
123 type = "app";
124 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
125 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
126 set -eux
127 ${script}
128 '').outPath;
129 };
130 in
131 {
132 # Example: nix run .#mermet.switch
133 "switch" = scriptApp "switch" [ ] (''
134 set -o pipefail
135 shopt -s globstar
136
137 chmod -R g-rwx,o-rwx **/*.gpg
138 trap 'git reset **/*.gpg' EXIT
139 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
140
141 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
142 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
143
144 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
145 '' +
146 # TODO: make it a LoadCredentialEncrypted= in nix-daemon.service
147 lib.optionalString config.boot.initrd.network.ssh.enable ''
148 # Send the SSH key of the initrd
149 gpg --decrypt 'hosts/${hostName}/ssh/initrd.key.gpg' |
150 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
151 '' + ''
152 if ssh ${target} set -x ';' \
153 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
154 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
155 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
156 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
157 sleep $((10 * 60))
158 ${profile}/bin/switch-to-configuration switch
159 systemctl reboot
160 '\'''' '&&' \
161 ${build.toplevel}/bin/switch-to-configuration test
162 then
163 ssh ${target} -o ControlPath=none set -x ';' \
164 systemctl stop nixos-fallback.service ';' \
165 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
166 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
167 nix-env --delete-generations 7d --profile ${profile}
168 else
169 tput rev
170 echo WARNING: switch-to-configuration was not registered at boot
171 tput sgr0
172 ssh ${target} -o ControlPath=none set -x ';' \
173 systemctl stop nixos-fallback.service
174 false
175 fi
176 '');
177 # Example: nix run .#carotte.install-sd
178 # DELETEME: quite useless, better to use nixos-install
179 "install-sd" = scriptApp "install-sd" [ zstd ] ''
180 set -o pipefail
181 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
182 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
183 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
184 '';
185 }
186 )
187 inputs.self.nixosConfigurations
188 );
189
190 # nix flake check
191 checks = forAllSystems (args: with args; {
192 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
193 src = inputs.self;
194 hooks = {
195 nixpkgs-fmt.enable = true;
196 };
197 };
198 });
199
200 # nix fmt
201 formatter = forAllSystems ({ pkgs, ... }:
202 inputs.nix-formatter-pack.lib.mkFormatter {
203 inherit pkgs;
204 config = {
205 tools = {
206 deadnix.enable = true;
207 nixpkgs-fmt.enable = true;
208 statix.enable = true;
209 };
210 };
211 }
212 );
213
214 };
215 }