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