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