From 5da80d3c522aaab9795e3eb333725dea85a03e80 Mon Sep 17 00:00:00 2001 From: Kat Inskip Date: Fri, 27 Jan 2023 15:20:34 -0800 Subject: [PATCH] feat: overlays + pkgs --- .envrc | 1 + .gitignore | 1 + common/nix.nix | 10 ++- common/overlay.nix | 10 ++- flake.lock | 48 +++++++---- flake.nix | 16 +++- formatter.nix | 8 ++ nixos/{common.nix => common/grub.nix} | 0 nixos/common/nix-index.nix | 3 + nixos/{ => common}/nix.nix | 5 ++ outputs.nix | 10 ++- overlays.nix | 7 ++ pkgs.nix | 9 ++ shells/default.nix | 5 +- shells/repo.nix | 25 ++++-- std.nix | 120 ++++++++++++++------------ systems/default.nix | 23 ++++- 17 files changed, 210 insertions(+), 91 deletions(-) create mode 100644 formatter.nix rename nixos/{common.nix => common/grub.nix} (100%) create mode 100644 nixos/common/nix-index.nix rename nixos/{ => common}/nix.nix (65%) create mode 100644 overlays.nix create mode 100644 pkgs.nix diff --git a/.envrc b/.envrc index 0b56b103..94c6e7a4 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,3 @@ export REPO_ROOT="$(pwd)" +export REPO_HOSTNAME=$(hostname -s) use flake diff --git a/.gitignore b/.gitignore index f411f4d7..c68a9ceb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ venv/ .direnv/ result /kittywitch +data/ diff --git a/common/nix.nix b/common/nix.nix index 55cf7f4c..c416e2d8 100644 --- a/common/nix.nix +++ b/common/nix.nix @@ -3,13 +3,17 @@ lib, std, inputs, - tree, ... }: let inherit (std) set tuple list; inherit (lib.strings) versionAtLeast; - renameAttrs = names: set.remap ({_0, _1}: tuple.tuple2 (names.${_0} or _0) _1); - renameAttr = oldName: newName: renameAttrs { ${oldName} = newName; }; + renameAttrs = names: + set.remap ({ + _0, + _1, + }: + tuple.tuple2 (names.${_0} or _0) _1); + renameAttr = oldName: newName: renameAttrs {${oldName} = newName;}; in { nix = { nixPath = set.mapToValues (name: flake: "${name}=${flake.outPath}") (renameAttr "self" "kat" inputs); diff --git a/common/overlay.nix b/common/overlay.nix index 501a8555..33ebbfd8 100644 --- a/common/overlay.nix +++ b/common/overlay.nix @@ -1,7 +1,9 @@ -{inputs, ...}: { +{ + inputs, + tree, + ... +}: { nixpkgs = { - overlays = map (path: import "${path}/overlay.nix") [ - inputs.arcexprs - ]; + overlays = import tree.overlays {inherit inputs;}; }; } diff --git a/flake.lock b/flake.lock index 3567a478..2e602ea8 100644 --- a/flake.lock +++ b/flake.lock @@ -59,6 +59,32 @@ "type": "github" } }, + "deploy-rs": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "nixpkgs": [ + "nixpkgs" + ], + "utils": [ + "utils" + ] + }, + "locked": { + "lastModified": 1674127017, + "narHash": "sha256-QO1xF7stu5ZMDLbHN30LFolMAwY6TVlzYvQoUs1RD68=", + "owner": "serokell", + "repo": "deploy-rs", + "rev": "8c9ea9605eed20528bf60fae35a2b613b901fd77", + "type": "github" + }, + "original": { + "owner": "serokell", + "repo": "deploy-rs", + "type": "github" + } + }, "empty": { "locked": { "lastModified": 1630400035, @@ -95,7 +121,9 @@ "nixpkgs": [ "nixpkgs" ], - "utils": "utils" + "utils": [ + "utils" + ] }, "locked": { "lastModified": 1674041176, @@ -219,6 +247,7 @@ "inputs": { "arcexprs": "arcexprs", "darwin": "darwin", + "deploy-rs": "deploy-rs", "empty": "empty", "flake-compat": "flake-compat", "home-manager": "home-manager", @@ -230,7 +259,7 @@ "scalpel": "scalpel", "std": "std", "tree": "tree", - "utils": "utils_2" + "utils": "utils" } }, "rust-overlay": { @@ -333,21 +362,6 @@ "repo": "flake-utils", "type": "github" } - }, - "utils_2": { - "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 2b85fe3e..906e54e9 100644 --- a/flake.nix +++ b/flake.nix @@ -14,10 +14,22 @@ empty.url = "github:input-output-hk/empty-flake"; # self-explanatory nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + # deployments + deploy-rs = { + url = "github:serokell/deploy-rs"; + inputs = { + flake-compat.follows = "flake-compat"; + nixpkgs.follows = "nixpkgs"; + utils.follows = "utils"; + }; + }; # self-explanatory home-manager = { url = "github:nix-community/home-manager/master"; - inputs.nixpkgs.follows = "nixpkgs"; + inputs = { + nixpkgs.follows = "nixpkgs"; + utils.follows = "utils"; + }; }; # self-explanatory darwin = { @@ -76,5 +88,5 @@ flake = false; }; }; - outputs = inputs: import ./outputs.nix { inherit inputs; }; + outputs = inputs: import ./outputs.nix {inherit inputs;}; } diff --git a/formatter.nix b/formatter.nix new file mode 100644 index 00000000..4b0eccf9 --- /dev/null +++ b/formatter.nix @@ -0,0 +1,8 @@ +{ + inputs, + pkgs, + ... +}: +inputs.utils.lib.eachDefaultSystem (system: { + ${system} = pkgs.${system}.alejandra; +}) diff --git a/nixos/common.nix b/nixos/common/grub.nix similarity index 100% rename from nixos/common.nix rename to nixos/common/grub.nix diff --git a/nixos/common/nix-index.nix b/nixos/common/nix-index.nix new file mode 100644 index 00000000..5fbc4412 --- /dev/null +++ b/nixos/common/nix-index.nix @@ -0,0 +1,3 @@ +_: { + programs.command-not-found.enable = false; +} diff --git a/nixos/nix.nix b/nixos/common/nix.nix similarity index 65% rename from nixos/nix.nix rename to nixos/common/nix.nix index b6ba949f..6f986fe6 100644 --- a/nixos/nix.nix +++ b/nixos/common/nix.nix @@ -1,6 +1,11 @@ {lib, ...}: let inherit (lib.modules) mkDefault; in { + boot.loader = { + grub.configurationLimit = 8; + systemd-boot.configurationLimit = 8; + }; + nix.gc = { automatic = mkDefault true; dates = mkDefault "weekly"; diff --git a/outputs.nix b/outputs.nix index 81ba6ca3..0b4095a0 100644 --- a/outputs.nix +++ b/outputs.nix @@ -1,8 +1,12 @@ -{ inputs }: let +{inputs}: let inherit (inputs.nixpkgs) lib; std = import ./std.nix {inherit inputs;}; tree = import ./tree.nix {inherit inputs;}; systems = import ./systems {inherit inputs tree lib std;}; - shells = import ./shells {inherit inputs tree lib std;}; + shells = import ./shells {inherit inputs tree lib std pkgs;}; + inherit (import ./pkgs.nix {inherit inputs tree lib;}) pkgs; + formatter = import ./formatter.nix {inherit inputs pkgs;}; + inherit (std) set; + checks = set.map (_: deployLib: deployLib.deployChecks inputs.self.deploy) inputs.deploy-rs.lib; in - {inherit inputs tree lib std;} // systems // shells + {inherit inputs tree lib std pkgs checks formatter;} // systems // shells diff --git a/overlays.nix b/overlays.nix new file mode 100644 index 00000000..e7de77dd --- /dev/null +++ b/overlays.nix @@ -0,0 +1,7 @@ +{inputs, ...}: +[ + inputs.deploy-rs.overlay +] +++ map (path: import "${path}/overlay.nix") [ + inputs.arcexprs +] diff --git a/pkgs.nix b/pkgs.nix new file mode 100644 index 00000000..045cca5b --- /dev/null +++ b/pkgs.nix @@ -0,0 +1,9 @@ +{ + lib, + tree, + inputs, + ... +}: let + overlays = import tree.overlays {inherit inputs;}; +in + inputs.utils.lib.eachDefaultSystem (system: {pkgs = import inputs.nixpkgs {inherit system overlays;};}) diff --git a/shells/default.nix b/shells/default.nix index 520203d9..b62e989b 100644 --- a/shells/default.nix +++ b/shells/default.nix @@ -3,6 +3,7 @@ tree, inputs, std, + pkgs, ... }: let inherit (std) set; @@ -10,9 +11,9 @@ in inputs.utils.lib.eachDefaultSystem (system: { devShells = let shells = set.map (_: path: - import path rec { + import path { inherit tree inputs system lib std; - pkgs = inputs.nixpkgs.legacyPackages.${system}; + pkgs = pkgs.${system}; }) tree.shells; in diff --git a/shells/repo.nix b/shells/repo.nix index 4c1d8ee9..1e3cd25c 100644 --- a/shells/repo.nix +++ b/shells/repo.nix @@ -5,20 +5,33 @@ ... }: with pkgs; let - repo = import ../outputs.nix { inherit inputs; }; - inherit (std) set list; + repo = import ../outputs.nix {inherit inputs;}; + inherit (std) set; repoShell = mkShell { nativeBuildInputs = [ + fd # fd, better fine! + ripgrep # rg, better grep! go # Required for pulumi pulumi-bin # Infrastructure as code deadnix # dead-code scanner alejandra # code formatter statix # anti-pattern finder + deploy-rs.deploy-rs # deployment system ] ++ set.values (set.map (name: _: (pkgs.writeShellScriptBin "${name}-rebuild" '' - darwin-rebuild switch --flake $REPO_ROOT#${name} - '')) - repo.darwinConfigurations); + darwin-rebuild switch --flake $REPO_ROOT#${name} + '')) + repo.darwinConfigurations); + shellHook = '' + echo -e "\e[39m\e[1m$USER@$REPO_HOSTNAME - \e[35m''$(realpath --relative-to=../ ./nixos/)\e[0m" + echo -e "\e[35mRunning alejandra\e[0m" + alejandra -cq $(fd -e nix) + echo -e "\e[35mRunning statix\e[0m" + statix check + echo -e "\e[35mRunning deadnix\e[0m" + deadnix + ''; }; -in repoShell +in + repoShell diff --git a/std.nix b/std.nix index 77500544..15f9c7df 100644 --- a/std.nix +++ b/std.nix @@ -1,63 +1,77 @@ -{ inputs, ... }: let +{inputs, ...}: let std = let baseStd = inputs.std.lib; inherit (baseStd) set function list bool types optional; mergeWith = let append = { - path - , values - , canMerge - , mapToSet + path, + values, + canMerge, + mapToSet, }: let - mergeWith' = values: mergeWith { - inherit canMerge mapToSet path; - sets = list.map (v: (mapToSet path v).value) values; - }; + mergeWith' = values: + mergeWith { + inherit canMerge mapToSet path; + sets = list.map (v: (mapToSet path v).value) values; + }; mergeUntil = list.findIndex (function.not (canMerge path)) values; len = list.length values; - in if len == 0 then { } - else if len == 1 then list.unsafeHead values - else if list.all (canMerge path) values then mergeWith' values - else optional.match mergeUntil { - just = i: let - split = list.splitAt i values; - in if i > 0 - then mergeWith' split._0 - else list.unsafeHead values; - nothing = list.unsafeHead values; + in + if len == 0 + then {} + else if len == 1 + then list.unsafeHead values + else if list.all (canMerge path) values + then mergeWith' values + else + optional.match mergeUntil { + just = i: let + split = list.splitAt i values; + in + if i > 0 + then mergeWith' split._0 + else list.unsafeHead values; + nothing = list.unsafeHead values; + }; + in + { + canMerge ? path: v: optional.isJust (mapToSet path v), + mapToSet ? _: v: bool.toOptional (types.attrs.check v) v, + path ? [], + sets, + }: + set.mapZip (name: values: + append { + path = path ++ list.One name; + inherit canMerge mapToSet values; + }) + sets; + merge = sets: + mergeWith { + inherit sets; }; - in { - canMerge ? path: v: optional.isJust (mapToSet path v), - mapToSet ? path: v: bool.toOptional (types.attrs.check v) v, - path ? [ ], - sets - }: set.mapZip (name: values: append { - path = path ++ list.One name; - inherit canMerge mapToSet values; - }) sets; - merge = sets: mergeWith { - inherit sets; - }; - in merge [ - baseStd - { - function = { - pipe = list.foldl' (function.flip function.compose) function.id; - }; - set = { - inherit merge mergeWith; - remap = f: s: set.fromList (list.map f (set.toList s)); - recursiveMap = f: s: let - recurse = str: s: let - g = str1: str2: - if types.attrs.check str2 - then f (str ++ [str1]) (recurse (str ++ [str1]) str2) - else f (str ++ [str1]) str2; + in + merge [ + baseStd + { + function = { + pipe = list.foldl' (function.flip function.compose) function.id; + }; + set = { + inherit merge mergeWith; + remap = f: s: set.fromList (list.map f (set.toList s)); + recursiveMap = f: s: let + recurse = str: s: let + g = str1: str2: + if types.attrs.check str2 + then f (str ++ [str1]) (recurse (str ++ [str1]) str2) + else f (str ++ [str1]) str2; + in + set.map g s; in - set.map g s; - in - f [] (recurse [] s); - }; - } - ]; -in std + f [] (recurse [] s); + }; + } + ]; +in + std diff --git a/systems/default.nix b/systems/default.nix index 213a8e32..cfa3e6d3 100644 --- a/systems/default.nix +++ b/systems/default.nix @@ -7,7 +7,7 @@ }: let # The purpose of this file is to set up the host module which allows assigning of the system, e.g. aarch64-linux and the builder used with less pain. inherit (lib.modules) evalModules; - inherit (std) string list function types bool optional set; + inherit (std) string types optional set; defaultSpecialArgs = { inherit inputs tree std; }; @@ -106,6 +106,27 @@ processHost = name: cfg: let host = cfg.config; in { + deploy.nodes = set.merge [ + (set.optional (host.folder == "nixos") { + ${name} = { + profiles.system = { + user = "root"; + path = inputs.deploy-rs.lib.x86_64-linux.activate.nixos inputs.self.nixosConfigurations.${name}; + }; + hostname = "${name}.inskip.me"; + sshOpts = ["-p" "${builtins.toString (builtins.head inputs.self.nixosConfigurations.${name}.config.services.openssh.ports)}"]; + sshUser = "kat"; + user = "root"; + autoRollback = true; + magicRollback = true; + }; + }) + (set.optional (host.folder == "nixos" && host.arch != "x86_64") { + ${name} = { + remoteBuild = true; + }; + }) + ]; "${host.folder}Configurations".${name} = let hostConfig = host.builder { inherit (host) system modules specialArgs;