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