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