]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: update hash of PR#236729
[julm/julm-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.flake = false;
4 doom-emacs.url = "github:hlissner/doom-emacs";
5 home-manager.inputs.nixpkgs.follows = "nixpkgs";
6 home-manager.url = "github:nix-community/home-manager/release-23.05";
7 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
8 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
9 nixpkgs.url = "github:NixOS/nixpkgs/release-23.05";
10 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
11 pre-commit-hooks.inputs.nixpkgs-stable.follows = "nixpkgs";
12 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
13 };
14
15 outputs = inputs:
16 let
17 remoteNixpkgsPatches = import nixpkgs/patches.nix;
18 localNixpkgsPatches = [
19 ];
20 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
21 nixpkgsPath = originPkgs.applyPatches {
22 name = "nixpkgs-patched";
23 src = inputs.nixpkgs.outPath;
24 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
25 postPatch = ''
26 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
27 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
28 sort | sha256sum | cut -c -7)
29 echo "-patch-$patch" >.version-suffix
30 '';
31 };
32 profile = "/nix/var/nix/profiles/system";
33 inherit (inputs.nixpkgs) lib;
34 #nixosSystem = lib.nixosSystem;
35 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
36 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
37 inherit system;
38 #pkgs = inputs.nixpkgs.legacyPackages.${system};
39 pkgs = import nixpkgsPath {
40 inherit system;
41 overlays = import nixpkgs/overlays.nix;
42 };
43 });
44 in
45 {
46 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
47 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
48 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
49 # nix eval --raw .#nixosConfigurations.oignon.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 system = null;
54 specialArgs = {
55 # Required to avoid infinite recursion
56 # when `inputs` is used in `imports`.
57 inherit inputs;
58 };
59 modules = [
60 nixos/default.nix
61 (import (./hosts + "/${hostName}.nix"))
62 {
63 _module.args = {
64 inherit hostName;
65 hosts = inputs.self.nixosConfigurations;
66 host = inputs.self.nixosConfigurations.${hostName}._module.args;
67 };
68 nixpkgs.overlays = import nixpkgs/overlays.nix;
69 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
70 }
71 inputs.home-manager.nixosModules.home-manager
72 {
73 home-manager.useGlobalPkgs = true;
74 home-manager.useUserPackages = true;
75 home-manager.verbose = true;
76 home-manager.backupFileExtension = null;
77 home-manager.extraSpecialArgs = {
78 inherit hostName inputs;
79 };
80 }
81 ];
82 });
83
84 # nix -L develop or direnv allow
85 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
86 {
87 inherit pkgs inputs system nixpkgsPath;
88 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
89 });
90
91 # nix -L run .#oignon.switch
92 apps = forAllSystems ({ pkgs, system, ... }:
93 with builtins;
94 mapAttrs
95 (hostName: { config, ... }:
96 let
97 inherit (config.system) build;
98 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
99 scriptApp = scriptName: ps: script: {
100 type = "app";
101 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
102 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
103 set -eux
104 ${script}
105 '').outPath;
106 };
107 in
108 {
109 # Example: nix run .#aubergine.switch
110 "switch" = scriptApp "switch" [ ] ''
111 shopt -s globstar
112
113 chmod -R g-rwx,o-rwx **/*.gpg
114 trap 'git reset **/*.gpg' EXIT
115 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
116
117 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
118 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
119
120 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
121
122 if ssh ${target} set -x ';' \
123 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
124 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
125 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
126 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
127 sleep $((10 * 60))
128 ${profile}/bin/switch-to-configuration switch
129 systemctl reboot
130 '\'''' '&&' \
131 ${build.toplevel}/bin/switch-to-configuration test
132 then
133 ssh ${target} -o ControlPath=none set -x ';' \
134 systemctl stop nixos-fallback.service ';' \
135 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
136 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
137 nix-env --delete-generations 7d --profile ${profile}
138 else
139 tput rev
140 echo WARNING: switch-to-configuration was not registered at boot
141 tput sgr0
142 ssh ${target} -o ControlPath=none set -x ';' \
143 systemctl stop nixos-fallback.service
144 false
145 fi
146 '';
147 })
148 inputs.self.nixosConfigurations);
149
150 # nix flake check
151 checks = forAllSystems (args: with args; {
152 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
153 src = inputs.self;
154 hooks = {
155 nixpkgs-fmt.enable = true;
156 };
157 };
158 });
159
160 # nix fmt
161 formatter = forAllSystems ({ pkgs, ... }:
162 inputs.nix-formatter-pack.lib.mkFormatter {
163 inherit pkgs;
164 config = {
165 tools = {
166 deadnix.enable = true;
167 nixpkgs-fmt.enable = true;
168 statix.enable = true;
169 };
170 };
171 }
172 );
173 };
174 }