]> Git — Sourcephile - sourcephile-nix.git/blob - flake.nix
nix: format all .nix files
[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/fdebb81f45a1ba2c4afca5fd9f526e1653ad0949";
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 shopt -s globstar
134
135 chmod -R g-rwx,o-rwx **/*.gpg
136 trap 'git reset **/*.gpg' EXIT
137 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
138
139 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
140 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
141
142 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
143 '' +
144 # TODO: make it a LoadCredentialEncrypted= in nix-daemon.service
145 lib.optionalString config.boot.initrd.network.ssh.enable ''
146 # Send the SSH key of the initrd
147 gpg --decrypt 'hosts/${hostName}/ssh/initrd.key.gpg' |
148 ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
149 '' + ''
150 ssh ${target} set -x ';' \
151 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
152 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
153 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
154 sleep $((10 * 60))
155 ${profile}/bin/switch-to-configuration switch
156 systemctl reboot
157 '\'''' '&&' \
158 ${build.toplevel}/bin/switch-to-configuration test
159
160 ssh ${target} -o ControlPath=none set -x ';' \
161 systemctl stop nixos-fallback.service ';' \
162 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
163 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
164 nix-env --delete-generations 7d --profile ${profile}
165 '');
166 # Example: nix run .#carotte.install-sd
167 # DELETEME: quite useless, better to use nixos-install
168 "install-sd" = scriptApp "install-sd" [ zstd ] ''
169 set -o pipefail
170 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
171 unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
172 sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
173 '';
174 }
175 )
176 inputs.self.nixosConfigurations
177 );
178
179 # nix flake check
180 checks = forAllSystems (args: with args; {
181 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
182 src = inputs.self;
183 hooks = {
184 nixpkgs-fmt.enable = true;
185 };
186 };
187 });
188
189 # nix fmt
190 formatter = forAllSystems ({ pkgs, ... }:
191 inputs.nix-formatter-pack.lib.mkFormatter {
192 inherit pkgs;
193 config = {
194 tools = {
195 deadnix.enable = true;
196 nixpkgs-fmt.enable = true;
197 statix.enable = true;
198 };
199 };
200 }
201 );
202
203 };
204 }