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