]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: update to nixos-24.05
[julm/julm-nix.git] / flake.nix
1 {
2 /*
3 nixConfig = {
4 extra-substituters = [
5 "https://nix-community.cachix.org"
6 ];
7 extra-trusted-public-keys = [
8 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
9 ];
10 };
11 */
12
13 inputs = {
14 doom-emacs.flake = false;
15 doom-emacs.url = "github:hlissner/doom-emacs";
16 home-manager.inputs.nixpkgs.follows = "nixpkgs";
17 home-manager.url = "github:nix-community/home-manager/release-24.05";
18 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
19 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
20 nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
21 git-hooks.inputs.nixpkgs.follows = "nixpkgs";
22 git-hooks.inputs.nixpkgs-stable.follows = "nixpkgs";
23 git-hooks.url = "github:cachix/git-hooks.nix";
24 radicle-explorer.url = "git+https://seed.radicle.garden/z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git";
25 radicle-explorer.inputs.heartwood.follows = "heartwood";
26 heartwood.url = "git+https://seed.radicle.garden/z3gqcJUoA1n9HaHKufZs5FCSGazv5.git";
27 heartwood.inputs.nixpkgs.follows = "nixpkgs";
28 };
29
30 outputs = inputs:
31 let
32 remoteNixpkgsPatches = import nixpkgs/patches.nix;
33 localNixpkgsPatches = [
34 ];
35 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
36 nixpkgsPath = originPkgs.applyPatches {
37 name = "nixpkgs-patched";
38 src = inputs.nixpkgs.outPath;
39 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
40 postPatch = ''
41 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
42 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
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 = system:
50 import nixpkgs/overlays.nix ++ [
51 (finalPkgs: previousPkgs:
52 inputs.heartwood.packages.${system}
53 )
54 ];
55 #nixosSystem = lib.nixosSystem;
56 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
57 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
58 inherit system;
59 #pkgs = inputs.nixpkgs.legacyPackages.${system};
60 pkgs = import nixpkgsPath {
61 inherit system;
62 overlays = overlays system;
63 };
64 });
65 self = with lib.fileset; toSource {
66 root = ./.;
67 fileset =
68 let exts = [ "clear" "conf" "cred" "crt" "css" "el" "hs" "json" "nix" "patch" "pem" "pub" "sh" "theme" "vim" ]; in
69 unions [
70 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
71 (fileFilter (file: lib.any file.hasExt exts) ./homes)
72 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
73 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
74 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
75 (fileFilter (file: lib.any file.hasExt exts) ./share)
76 (fileFilter (file: lib.any file.hasExt exts) ./users)
77 ./shell.nix
78 ];
79 };
80 in
81 rec {
82 # nix -L build .#hello
83 packages = forAllSystems ({ pkgs, ... }: pkgs);
84
85 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
86 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
87 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
88 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
89 nixosConfigurations = lib.genAttrs
90 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))))
91 (hostName: nixosSystem {
92 system = null;
93 specialArgs = {
94 # Required to avoid infinite recursion
95 # when `inputs` is used in `imports`.
96 inherit inputs;
97 };
98 modules = [
99 nixos/default.nix
100 (import (self + "/hosts/${hostName}.nix"))
101 ({ config, ... }: {
102 _module.args = {
103 inherit hostName;
104 hosts = nixosConfigurations;
105 host = nixosConfigurations.${hostName}._module.args;
106 };
107 nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
108 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
109 "anydesk"
110 "cudatoolkit"
111 "geogebra"
112 "hplip"
113 "memtest86-efi"
114 "nvidia-settings"
115 "nvidia-x11"
116 ];
117 })
118 inputs.home-manager.nixosModules.home-manager
119 {
120 home-manager.useGlobalPkgs = true;
121 home-manager.useUserPackages = true;
122 home-manager.verbose = true;
123 home-manager.backupFileExtension = null;
124 home-manager.extraSpecialArgs = {
125 inherit hostName inputs;
126 };
127 }
128 ];
129 });
130
131 # nix -L develop or direnv allow
132 devShells = forAllSystems ({ pkgs, system, ... }:
133 {
134 default = pkgs.callPackage (self + "/shell.nix")
135 {
136 inherit pkgs inputs system nixpkgsPath;
137 inherit (checks.${system}.git-hooks-check) shellHook;
138 };
139 }
140 );
141
142 # nix -L run .#oignon.switch
143 apps = forAllSystems ({ pkgs, system, ... }:
144 with builtins;
145 mapAttrs
146 (hostName: { config, ... }:
147 let
148 inherit (config.system) build;
149 scriptApp = scriptName: ps: script: {
150 type = "app";
151 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
152 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
153 set -eux
154 ${script}
155 '').outPath;
156 };
157 in
158 {
159 # Example: nix run .#aubergine.switch
160 "switch" = scriptApp "switch" [ ] ''
161 shopt -s globstar
162
163 chmod -R g-rwx,o-rwx **/*.gpg
164 trap 'git reset **/*.gpg' EXIT
165 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
166
167 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
168 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
169
170 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
171
172 if ssh ${config.install.target} set -x ';' \
173 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
174 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
175 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
176 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
177 sleep $((10 * 60))
178 ${profile}/bin/switch-to-configuration switch
179 systemctl reboot
180 '\'''' '&&' \
181 ${build.toplevel}/bin/switch-to-configuration test
182 then
183 ssh ${config.install.target} -o ControlPath=none set -x ';' \
184 systemctl stop nixos-fallback.service ';' \
185 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
186 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
187 nix-env --delete-generations 7d --profile ${profile}
188 else
189 tput rev
190 echo WARNING: switch-to-configuration was not registered at boot
191 tput sgr0
192 ssh ${config.install.target} -o ControlPath=none set -x ';' \
193 systemctl stop nixos-fallback.service
194 false
195 fi
196 '';
197 })
198 nixosConfigurations);
199
200 # nix flake check
201 checks = forAllSystems (args: with args; {
202 git-hooks-check = inputs.git-hooks.lib.${system}.run {
203 src = self;
204 hooks = {
205 nixpkgs-fmt.enable = true;
206 };
207 };
208 });
209
210 # nix fmt
211 formatter = forAllSystems ({ pkgs, ... }:
212 inputs.nix-formatter-pack.lib.mkFormatter {
213 inherit pkgs;
214 config = {
215 tools = {
216 deadnix.enable = true;
217 nixpkgs-fmt.enable = true;
218 statix.enable = true;
219 };
220 };
221 }
222 );
223 };
224 }