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