]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
mermet: radicle: nginx: add radicle-explorer Web site
[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-23.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 import nixpkgs/overlays.nix ++
35 import (inputs.julm-nix + "/nixpkgs/overlays.nix") ++ [
36 (finalPkgs: previousPkgs:
37 inputs.julm-nix.inputs.heartwood.packages.${system} //
38 inputs.julm-nix.inputs.radicle-explorer.packages.${system}
39 )
40 ];
41 #nixosSystem = lib.nixosSystem;
42 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
43 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
44 inherit system;
45 #pkgs = inputs.nixpkgs.legacyPackages.${system};
46 pkgs = import nixpkgsPath { inherit system; overlays = overlays system; };
47 });
48 in
49 {
50 # nix -L build .#hello
51 packages = forAllSystems (args: with args; pkgs);
52
53 # nix -L build .#nixosConfigurations.mermet.config.system.build.toplevel
54 # nix -L build .#nixosConfigurations.mermet.config.boot.kernelPackages.kernel.configfile
55 # nix -L build .#nixosConfigurations.mermet.pkgs.hello
56 # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.ruleset
57 nixosConfigurations = lib.genAttrs
58 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
59 (hostName: nixosSystem
60 {
61 system = null;
62 specialArgs = {
63 # Required to avoid infinite recursion
64 # when `inputs` is used in `imports`.
65 inherit inputs;
66 };
67 modules = [
68 nixos/default.nix
69 (inputs.julm-nix + "/nixos/default.nix")
70 (inputs.self + "/hosts/${hostName}.nix")
71 ({ config, ... }: {
72 _module.args = {
73 inherit hostName;
74 hosts = inputs.self.nixosConfigurations;
75 host = inputs.self.nixosConfigurations.${hostName}._module.args;
76 info = import ./info.nix;
77 };
78 nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
79 #nixpkgs.buildPlatform = "x86_64-linux";
80 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" ];
81 })
82 inputs.nixpkgs.nixosModules.notDetected
83 inputs.home-manager.nixosModules.home-manager
84 {
85 home-manager.useGlobalPkgs = true;
86 home-manager.useUserPackages = true;
87 home-manager.verbose = true;
88 home-manager.backupFileExtension = null;
89 home-manager.extraSpecialArgs = {
90 inherit hostName inputs;
91 };
92 }
93 {
94 programs.ssh.knownHosts = {
95 carotte = {
96 extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
97 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
98 };
99 mermet = {
100 extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
101 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
102 };
103 losurdo = {
104 extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
105 publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
106 };
107 };
108 }
109 ];
110 }
111 );
112
113 # nix -L develop or direnv allow
114 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
115 {
116 inherit pkgs inputs system nixpkgsPath;
117 inherit (inputs.self.checks.${system}.git-hooks-check) shellHook;
118 });
119
120 # nix -L run .#mermet.switch
121 apps = forAllSystems ({ pkgs, system, ... }:
122 with builtins;
123 mapAttrs
124 (hostName: { config, pkgs, ... }:
125 let
126 inherit (config.system) build;
127 scriptApp = scriptName: ps: script: {
128 type = "app";
129 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
130 export PATH="${lib.makeBinPath ([ pkgs.buildPackages.coreutils ] ++ ps)}:$PATH"
131 set -eux
132 ${script}
133 '').outPath;
134 };
135 in
136 {
137 # Example: nix run .#mermet.switch
138 "switch" = scriptApp "switch" [ ] (
139 ''
140 set -o pipefail
141 shopt -s globstar
142
143 chmod -R g-rwx,o-rwx **/*.gpg
144 trap 'git reset **/*.gpg' EXIT
145 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
146
147 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
148 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
149
150 nix copy --to ssh://${config.install.target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
151
152 ${config.security.initrd.install}
153
154 if ssh ${config.install.target} set -x ';' \
155 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
156 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
157 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
158 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
159 sleep $((10 * 60))
160 ${profile}/bin/switch-to-configuration switch
161 systemctl reboot
162 '\'''' '&&' \
163 ${build.toplevel}/bin/switch-to-configuration test
164 then
165 ssh ${config.install.target} -o ControlPath=none set -x ';' \
166 systemctl stop nixos-fallback.service ';' \
167 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
168 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
169 nix-env --delete-generations 7d --profile ${profile}
170 else
171 tput rev
172 echo WARNING: switch-to-configuration was not registered at boot
173 tput sgr0
174 ssh ${config.install.target} -o ControlPath=none set -x ';' \
175 systemctl stop nixos-fallback.service
176 false
177 fi
178 ''
179 );
180 # Example: nix run .#carotte.install-sd
181 # DELETEME: quite useless, better to use nixos-install
182 "install-sd" = scriptApp "install-sd" [ pkgs.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 git-hooks-check = inputs.git-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 }