]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: add a description
[julm/julm-nix.git] / flake.nix
1 {
2 description = "julm's Nix configurations for hosts (NixOS) and homes (home-manager)";
3 /*
4 nixConfig = {
5 extra-substituters = [
6 "https://nix-community.cachix.org"
7 ];
8 extra-trusted-public-keys = [
9 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
10 ];
11 };
12 */
13
14 inputs = {
15 doom-emacs.flake = false;
16 doom-emacs.url = "github:hlissner/doom-emacs";
17 home-manager.inputs.nixpkgs.follows = "nixpkgs";
18 home-manager.url = "github:nix-community/home-manager/release-24.11";
19 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
20 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
21 nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
22 git-hooks.inputs.nixpkgs.follows = "nixpkgs";
23 git-hooks.inputs.nixpkgs-stable.follows = "nixpkgs";
24 git-hooks.url = "github:cachix/git-hooks.nix";
25 nixos-hardware.url = "github:NixOS/nixos-hardware/master";
26 };
27
28 outputs =
29 inputs:
30 let
31 remoteNixpkgsPatches = import nixpkgs/patches.nix;
32 localNixpkgsPatches = [
33 ];
34 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
35 nixpkgsPath = originPkgs.applyPatches {
36 name = "nixpkgs-patched";
37 src = inputs.nixpkgs.outPath;
38 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
39 postPatch = ''
40 patch=$(printf '%s\n' ${
41 builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
42 } |
43 sort | sha256sum | cut -c -7)
44 echo "-patch-$patch" >.version-suffix
45 '';
46 };
47 profile = "/nix/var/nix/profiles/system";
48 inherit (inputs.nixpkgs) lib;
49 overlays =
50 system:
51 import nixpkgs/overlays.nix
52 ++ [
53 ];
54 #nixosSystem = lib.nixosSystem;
55 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
56 forAllSystems =
57 f:
58 lib.genAttrs lib.systems.flakeExposed (
59 system:
60 f rec {
61 inherit system;
62 #pkgs = inputs.nixpkgs.legacyPackages.${system};
63 pkgs = import nixpkgsPath {
64 inherit system;
65 overlays = overlays system;
66 };
67 }
68 );
69 self =
70 with lib.fileset;
71 toSource {
72 root = ./.;
73 fileset =
74 let
75 exts = [
76 "clear"
77 "conf"
78 "cred"
79 "crt"
80 "css"
81 "el"
82 "hs"
83 "json"
84 "nix"
85 "patch"
86 "pem"
87 "pub"
88 "sh"
89 "theme"
90 "vim"
91 ];
92 in
93 unions [
94 (fileFilter (file: lib.any file.hasExt exts) ./domains)
95 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
96 (fileFilter (file: lib.any file.hasExt exts) ./homes)
97 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
98 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
99 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
100 (fileFilter (file: lib.any file.hasExt exts) ./users)
101 ./shell.nix
102 ];
103 };
104 in
105 rec {
106 # nix -L build .#hello
107 packages = forAllSystems ({ pkgs, ... }: pkgs);
108
109 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
110 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
111 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
112 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
113 nixosConfigurations =
114 lib.genAttrs
115 (builtins.attrNames (
116 lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))
117 ))
118 (
119 hostName:
120 nixosSystem {
121 system = null;
122 specialArgs = {
123 # Required to avoid infinite recursion
124 # when `inputs` is used in `imports`.
125 inherit inputs;
126 };
127 modules = [
128 nixos/default.nix
129 (import (self + "/hosts/${hostName}.nix"))
130 (
131 { config, ... }:
132 {
133 _module.args = {
134 inherit hostName;
135 hosts = nixosConfigurations;
136 host = nixosConfigurations.${hostName}._module.args;
137 };
138 nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
139 nixpkgs.config.permittedInsecurePackages = [
140 # Still needed for chatty
141 # See https://github.com/NixOS/nixpkgs/pull/334638#issuecomment-2289025802%3E
142 # and https://gitlab.gnome.org/World/Chatty/-/issues/932
143 "olm-3.2.16"
144 ];
145 nixpkgs.config.allowUnfreePredicate =
146 pkg:
147 builtins.elem (lib.getName pkg) [
148 "anydesk"
149 "canon-cups-ufr2"
150 "cudatoolkit"
151 "geogebra"
152 "hplip"
153 "memtest86-efi"
154 "nvidia-settings"
155 "nvidia-x11"
156 ];
157 }
158 )
159 inputs.home-manager.nixosModules.home-manager
160 {
161 home-manager.useGlobalPkgs = true;
162 home-manager.useUserPackages = true;
163 home-manager.verbose = true;
164 #home-manager.force = true;
165 home-manager.backupFileExtension = "old";
166 home-manager.extraSpecialArgs = {
167 inherit hostName inputs;
168 };
169 }
170 ];
171 }
172 );
173
174 # nix -L develop or direnv allow
175 devShells = forAllSystems (
176 { pkgs, system, ... }:
177 {
178 default = pkgs.callPackage (self + "/shell.nix") {
179 inherit
180 pkgs
181 inputs
182 system
183 nixpkgsPath
184 ;
185 inherit (checks.${system}.git-hooks-check) shellHook;
186 };
187 }
188 );
189
190 # nix -L run .#oignon.switch
191 apps = forAllSystems (
192 { pkgs, system, ... }:
193 with builtins;
194 mapAttrs (
195 hostName:
196 { config, ... }:
197 let
198 inherit (config.system) build;
199 scriptApp = scriptName: ps: script: {
200 type = "app";
201 program =
202 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
203 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
204 set -eux
205 ${script}
206 '').outPath;
207 };
208 in
209 {
210 # Example: nix run .#aubergine.switch
211 "switch" = scriptApp "switch" [ ] ''
212 shopt -s globstar
213
214 chmod -R g-rwx,o-rwx **/*.gpg
215 trap 'git reset **/*.gpg' EXIT
216 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
217
218 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
219 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
220
221 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
222
223 if ssh ${config.install.target} set -x ';' \
224 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
225 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
226 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
227 PATH=${
228 with pkgs;
229 lib.makeBinPath [
230 coreutils
231 nix
232 systemd
233 ]
234 }
235 sleep $((10 * 60))
236 ${profile}/bin/switch-to-configuration switch
237 systemctl reboot
238 '\'''' '&&' \
239 ${build.toplevel}/bin/switch-to-configuration test
240 then
241 ssh ${config.install.target} -o ControlPath=none set -x ';' \
242 systemctl stop nixos-fallback.service ';' \
243 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
244 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
245 nix-env --delete-generations 7d --profile ${profile}
246 else
247 tput rev
248 echo WARNING: switch-to-configuration was not registered at boot
249 tput sgr0
250 ssh ${config.install.target} -o ControlPath=none set -x ';' \
251 systemctl stop nixos-fallback.service
252 false
253 fi
254 '';
255 }
256 ) nixosConfigurations
257 );
258
259 # nix flake check
260 checks = forAllSystems (
261 args: with args; {
262 git-hooks-check = inputs.git-hooks.lib.${system}.run {
263 src = self;
264 hooks = {
265 nixfmt-rfc-style.enable = true;
266 };
267 };
268 }
269 );
270
271 # nix fmt
272 formatter = forAllSystems (
273 { pkgs, ... }:
274 inputs.nix-formatter-pack.lib.mkFormatter {
275 inherit pkgs;
276 config = {
277 tools = {
278 deadnix.enable = true;
279 nixfmt-rfc-style.enable = true;
280 statix.enable = true;
281 };
282 };
283 }
284 );
285 };
286 }