From 6fa74fead8f04a6c5da214da6d9c0c9fbc42e1e7 Mon Sep 17 00:00:00 2001 From: Hektor Misplon Date: Fri, 6 Nov 2026 13:17:50 +0100 Subject: [PATCH] refactor: rename utils to lib' --- deploy/colmena.nix | 16 ++++------- deploy/deploy-rs.nix | 8 +++--- flake.nix | 28 +++++-------------- home/modules/ai-tools/opencode.nix | 6 ++-- home/modules/anki/default.nix | 6 ++-- home/modules/default.nix | 4 +-- home/modules/deploy/default.nix | 9 +++--- home/modules/git/default.nix | 6 ++-- home/modules/shell/bash.nix | 4 +-- home/modules/ssh/default.nix | 11 ++++---- home/modules/taskwarrior/default.nix | 6 ++-- {utils => lib}/default.nix | 4 ++- {utils => lib}/fs.nix | 0 lib/hosts.nix | 23 +++++++++++++++ lib/secrets.nix | 34 ++++++++++++++++++++++ modules/common/home-manager.nix | 8 ++---- modules/secrets/default.nix | 4 +-- modules/ssh/authorized-keys.nix | 6 ++-- modules/ssh/known-hosts.nix | 7 ++--- modules/wol/default.nix | 9 +++--- utils/hosts.nix | 19 ------------- utils/secrets.nix | 42 ---------------------------- 22 files changed, 116 insertions(+), 144 deletions(-) rename {utils => lib}/default.nix (68%) rename {utils => lib}/fs.nix (100%) create mode 100644 lib/hosts.nix create mode 100644 lib/secrets.nix delete mode 100644 utils/hosts.nix delete mode 100644 utils/secrets.nix diff --git a/deploy/colmena.nix b/deploy/colmena.nix index 74bb3a5..07c0b1c 100644 --- a/deploy/colmena.nix +++ b/deploy/colmena.nix @@ -5,20 +5,20 @@ let inherit (inputs.nixpkgs) lib; - utils = import ../utils { inherit lib; }; + lib' = import ../lib { inherit lib; }; # exclude hosts that need auto rollback (until `colmena` supports it) for # now, we use `deploy-rs` (which has magic rollback) for those (also see ./deploy-rs.nix) # # see # hostnames = lib.filter ( - hostname: !((utils.hostMeta ../hosts/${hostname}).deploy.autoRollback or false) - ) (utils.dirNames ../hosts); + hostname: !((lib'.hostMeta hostname).deploy.autoRollback or false) + ) lib'.nixosHosts; mkNode = hostname: let - meta = utils.hostMeta ../hosts/${hostname}; + meta = lib'.hostMeta hostname; isArm = meta.system == "aarch64-linux"; in { @@ -40,13 +40,9 @@ inputs.colmena.lib.makeHive ( meta = { nixpkgs = import inputs.nixpkgs { localSystem = "x86_64-linux"; }; specialArgs = { - inherit inputs; + inherit inputs lib'; outputs = self; - myUtils = utils; - paths = { - dots = ../dots; - hosts = ../hosts; - }; + dotsPath = ../dots; }; }; } diff --git a/deploy/deploy-rs.nix b/deploy/deploy-rs.nix index 515585e..7b52825 100644 --- a/deploy/deploy-rs.nix +++ b/deploy/deploy-rs.nix @@ -5,18 +5,18 @@ let inherit (inputs.nixpkgs) lib; - utils = import ../utils { inherit lib; }; + lib' = import ../lib { inherit lib; }; # only include hosts that need auto rollback (until `colmena` supports it) # `colmena` is used for other hosts (see ./colmena.nix) hostnames = lib.filter ( - hostname: (utils.hostMeta ../hosts/${hostname}).deploy.autoRollback or false - ) (utils.dirNames ../hosts); + hostname: (lib'.hostMeta hostname).deploy.autoRollback or false + ) lib'.nixosHosts; mkNode = hostname: let - meta = utils.hostMeta ../hosts/${hostname}; + meta = lib'.hostMeta hostname; in { inherit hostname; diff --git a/flake.nix b/flake.nix index e6aef27..66fb2e6 100644 --- a/flake.nix +++ b/flake.nix @@ -74,15 +74,9 @@ ... }@inputs: let - inherit (self) outputs; inherit (inputs.nixpkgs) lib; - myUtils = import ./utils { inherit lib; }; - hostDirNames = myUtils.dirNames ./hosts; + lib' = import ./lib { inherit lib; }; system = "x86_64-linux"; - paths = { - dots = ./dots; - hosts = ./hosts; - }; gitHooks = import ./git-hooks.nix { inherit nixpkgs git-hooks system; src = ./.; @@ -90,22 +84,20 @@ in { nixosConfigurations = - (lib.genAttrs hostDirNames ( + (lib.genAttrs lib'.nixosHosts ( host: nixpkgs.lib.nixosSystem { modules = [ ./hosts/${host} { - nixpkgs.hostPlatform = (myUtils.hostMeta ./hosts/${host}).system; + nixpkgs.hostPlatform = (lib'.hostMeta host).system; host.name = host; } ]; specialArgs = { inherit inputs - outputs - paths - myUtils + lib' ; }; } @@ -123,9 +115,7 @@ specialArgs = { inherit inputs - outputs - paths - myUtils + lib' ; }; }; @@ -141,9 +131,7 @@ specialArgs = { inherit inputs - outputs - paths - myUtils + lib' ; }; }; @@ -160,9 +148,7 @@ osConfig = null; inherit inputs - outputs - paths - myUtils + lib' ; }; }; diff --git a/home/modules/ai-tools/opencode.nix b/home/modules/ai-tools/opencode.nix index eda4012..7366347 100644 --- a/home/modules/ai-tools/opencode.nix +++ b/home/modules/ai-tools/opencode.nix @@ -2,21 +2,21 @@ config, lib, pkgs, - myUtils, + lib', osConfig ? null, inputs ? null, ... }: let - sops = myUtils.sopsAvailability config osConfig; + sops = lib'.sopsAvailability config osConfig; standalone = osConfig == null; in { config = lib.mkIf config.ai-tools.opencode.enable ( lib.optionalAttrs standalone { sops = { - secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" { + secrets = lib'.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null { opencode = [ "api-key" ]; }; templates."opencode/auth.json" = { diff --git a/home/modules/anki/default.nix b/home/modules/anki/default.nix index 913927f..a9064e8 100644 --- a/home/modules/anki/default.nix +++ b/home/modules/anki/default.nix @@ -2,7 +2,7 @@ config, lib, pkgs, - myUtils, + lib', osConfig ? null, inputs ? null, ... @@ -10,7 +10,7 @@ let cfg = config.anki; - sops = myUtils.sopsAvailability config osConfig; + sops = lib'.sopsAvailability config osConfig; standalone = osConfig == null; in { @@ -18,7 +18,7 @@ in config = lib.mkIf cfg.enable ( lib.optionalAttrs standalone { - sops.secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" { + sops.secrets = lib'.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null { anki = [ "sync-user" "sync-key" diff --git a/home/modules/default.nix b/home/modules/default.nix index 8146a86..6e314c6 100644 --- a/home/modules/default.nix +++ b/home/modules/default.nix @@ -2,14 +2,14 @@ config, lib, pkgs, - myUtils, + lib', ... }: { imports = let - dirs = myUtils.dirNames ./.; + dirs = lib'.dirNames ./.; hasDef = name: builtins.pathExists ./${name}/default.nix; in map (name: ./${name}) (builtins.filter hasDef dirs); diff --git a/home/modules/deploy/default.nix b/home/modules/deploy/default.nix index 4a8eedf..8a2d800 100644 --- a/home/modules/deploy/default.nix +++ b/home/modules/deploy/default.nix @@ -3,23 +3,22 @@ lib, pkgs, inputs, - myUtils, - paths, + lib', ... }: let cfg = config.deploy; - hostNames = myUtils.dirNames paths.hosts; + inherit (lib') hostNames; hostsWithKeys = lib.filter ( - hostname: builtins.pathExists (paths.hosts + "/${hostname}/ssh_host.pub") + hostname: builtins.pathExists (lib'.hostsPath + "/${hostname}/ssh_host.pub") ) hostNames; colmena = inputs.colmena.packages.${pkgs.stdenv.hostPlatform.system}.colmena; deployRs = inputs.deploy-rs.packages.${pkgs.stdenv.hostPlatform.system}.default; - hostTags = hostname: (myUtils.hostMeta (paths.hosts + "/${hostname}")).tags; + hostTags = hostname: (lib'.hostMeta hostname).tags; nodeTagsDecl = '' declare -A _colmena_node_tags=( ${lib.concatMapStringsSep "\n" ( diff --git a/home/modules/git/default.nix b/home/modules/git/default.nix index 2f7ad97..665dd59 100644 --- a/home/modules/git/default.nix +++ b/home/modules/git/default.nix @@ -2,14 +2,14 @@ config, inputs, lib, - myUtils, + lib', osConfig ? null, ... }: let cfg = config.git; - sops = myUtils.sopsAvailability config osConfig; + sops = lib'.sopsAvailability config osConfig; standalone = osConfig == null; personalEmail = sops.templates.".gitconfig.email".path; @@ -50,7 +50,7 @@ in config = lib.mkIf cfg.enable ( lib.optionalAttrs standalone { sops = { - secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" { + secrets = lib'.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null { email = [ "personal" "work" diff --git a/home/modules/shell/bash.nix b/home/modules/shell/bash.nix index 8139675..df28144 100644 --- a/home/modules/shell/bash.nix +++ b/home/modules/shell/bash.nix @@ -1,7 +1,7 @@ { config, lib, - paths, + lib', ... }: @@ -66,6 +66,6 @@ in ''; }; - home.sessionPath = lib.optional cfg.addBinToPath "${paths.dots}/.bin"; + home.sessionPath = lib.optional cfg.addBinToPath "${lib'.dotsPath}/.bin"; }; } diff --git a/home/modules/ssh/default.nix b/home/modules/ssh/default.nix index 13d5920..3865fdc 100644 --- a/home/modules/ssh/default.nix +++ b/home/modules/ssh/default.nix @@ -2,17 +2,16 @@ config, lib, pkgs, - myUtils, - paths, + lib', ... }: let cfg = config.ssh; - hostNames = myUtils.dirNames paths.hosts; + hostsWithKeys = lib.filter ( - hostname: builtins.pathExists (paths.hosts + "/${hostname}/ssh_host.pub") - ) hostNames; + hostname: builtins.pathExists (lib'.hostsPath + "/${hostname}/ssh_host.pub") + ) lib'.hostNames; in { options.ssh.enable = lib.mkEnableOption "ssh"; @@ -37,7 +36,7 @@ in lib.genAttrs hostsWithKeys ( hostname: let - meta = myUtils.hostMeta (paths.hosts + "/${hostname}"); + meta = lib'.hostMeta hostname; in { User = meta.host.username; diff --git a/home/modules/taskwarrior/default.nix b/home/modules/taskwarrior/default.nix index 64e446a..9a062ea 100644 --- a/home/modules/taskwarrior/default.nix +++ b/home/modules/taskwarrior/default.nix @@ -2,14 +2,14 @@ config, lib, pkgs, - myUtils, + lib', osConfig ? null, inputs ? null, ... }: let - sops = myUtils.sopsAvailability config osConfig; + sops = lib'.sopsAvailability config osConfig; standalone = osConfig == null; syncHook = pkgs.writers.writePython3 "on-exit.sync.py" { flakeIgnore = [ "E501" ]; } '' @@ -64,7 +64,7 @@ in config = lib.mkIf config.taskwarrior.enable ( lib.optionalAttrs standalone { sops = { - secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" { + secrets = lib'.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null { taskwarrior = [ "sync-server-url" "sync-server-client-id" diff --git a/utils/default.nix b/lib/default.nix similarity index 68% rename from utils/default.nix rename to lib/default.nix index e8eed6e..dcf52b1 100644 --- a/utils/default.nix +++ b/lib/default.nix @@ -4,5 +4,7 @@ let fs = import ./fs.nix { inherit lib; }; hosts = import ./hosts.nix { inherit lib; }; secrets = import ./secrets.nix { inherit lib; }; + + dotsPath = ../dots; in -fs // hosts // secrets +fs // hosts // secrets // { inherit dotsPath; } diff --git a/utils/fs.nix b/lib/fs.nix similarity index 100% rename from utils/fs.nix rename to lib/fs.nix diff --git a/lib/hosts.nix b/lib/hosts.nix new file mode 100644 index 0000000..0b25ecf --- /dev/null +++ b/lib/hosts.nix @@ -0,0 +1,23 @@ +{ lib }: + +let + fs = import ./fs.nix { inherit lib; }; + + hostsPath = ../hosts; + hostMeta = + host: + let + metaPath = hostsPath + "/${host}/meta.nix"; + in + if builtins.pathExists metaPath then + import metaPath + else + throw "meta.nix required in ${toString metaPath}"; + hostNames = fs.dirNames hostsPath; +in +{ + inherit hostsPath hostMeta hostNames; + + adminHosts = builtins.filter (host: (hostMeta host).host.admin or false) hostNames; + nixosHosts = hostNames; +} diff --git a/lib/secrets.nix b/lib/secrets.nix new file mode 100644 index 0000000..c382dd9 --- /dev/null +++ b/lib/secrets.nix @@ -0,0 +1,34 @@ +{ lib }: + +{ + mkSopsSecrets = + sopsDir: owner: groups: + let + opts = lib.optionalAttrs (owner != null) { inherit owner; }; + mkGroup = + group: names: + lib.listToAttrs ( + map (name: { + name = "${group}/${name}"; + value = { + sopsFile = "${sopsDir}/${group}.yaml"; + key = name; + } + // opts; + }) names + ); + in + lib.mergeAttrsList (lib.mapAttrsToList mkGroup groups); + + sopsAvailability = + config: osConfig: + let + osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets; + hmSopsAvailable = config ? sops && config.sops ? secrets; + in + { + available = osSopsAvailable || hmSopsAvailable; + secrets = if osSopsAvailable then osConfig.sops.secrets else config.sops.secrets; + templates = if osSopsAvailable then osConfig.sops.templates else config.sops.templates; + }; +} diff --git a/modules/common/home-manager.nix b/modules/common/home-manager.nix index ec21459..7321048 100644 --- a/modules/common/home-manager.nix +++ b/modules/common/home-manager.nix @@ -1,8 +1,6 @@ { inputs, - outputs, - paths, - myUtils, + lib', config, lib, ... @@ -20,9 +18,7 @@ in extraSpecialArgs = { inherit inputs - outputs - paths - myUtils + lib' ; }; sharedModules = [ diff --git a/modules/secrets/default.nix b/modules/secrets/default.nix index bd394d4..d6723a8 100644 --- a/modules/secrets/default.nix +++ b/modules/secrets/default.nix @@ -3,7 +3,7 @@ inputs, pkgs, config, - myUtils, + lib', ... }: @@ -61,7 +61,7 @@ in config = lib.mkIf cfg.enable { sops = { - secrets = myUtils.mkSopsSecrets sopsDir system // myUtils.mkSopsUserSecrets sopsDir owner cfg.user; + secrets = lib'.mkSopsSecrets sopsDir null system // lib'.mkSopsSecrets sopsDir owner cfg.user; }; nix.settings.secret-key-files = lib.mkIf cfg.nixSigningKey.enable [ diff --git a/modules/ssh/authorized-keys.nix b/modules/ssh/authorized-keys.nix index f27caa8..6d708e6 100644 --- a/modules/ssh/authorized-keys.nix +++ b/modules/ssh/authorized-keys.nix @@ -1,13 +1,13 @@ { lib, config, - paths, + lib', ... }: let inherit (config.host) username; - adminHosts = (import ../../utils { inherit lib; }).adminHosts paths.hosts; + inherit (lib') adminHosts; in { options.ssh = { @@ -23,7 +23,7 @@ in map ( hostname: let - keyFile = paths.hosts + "/${hostname}/ssh_user.pub"; + keyFile = lib'.hostsPath + "/${hostname}/ssh_user.pub"; in lib.optionals (builtins.pathExists keyFile) (lib.splitString "\n" (builtins.readFile keyFile)) ) ((builtins.filter (h: h != config.host.name) adminHosts) ++ config.ssh.authorizedHosts) diff --git a/modules/ssh/known-hosts.nix b/modules/ssh/known-hosts.nix index 8cc646f..2f56551 100644 --- a/modules/ssh/known-hosts.nix +++ b/modules/ssh/known-hosts.nix @@ -1,14 +1,13 @@ { lib, config, - outputs, - paths, + lib', ... }: let - hosts = lib.attrNames outputs.nixosConfigurations; - keyPath = hostname: paths.hosts + "/${hostname}/ssh_host.pub"; + hosts = lib'.nixosHosts; + keyPath = hostname: lib'.hostsPath + "/${hostname}/ssh_host.pub"; hostsWithKeys = lib.filter (hostname: builtins.pathExists (keyPath hostname)) hosts; in { diff --git a/modules/wol/default.nix b/modules/wol/default.nix index d156d2e..ce1a09f 100644 --- a/modules/wol/default.nix +++ b/modules/wol/default.nix @@ -1,16 +1,15 @@ { lib, config, - outputs, - paths, - myUtils, + inputs, + lib', pkgs, ... }: let - hosts = myUtils.dirNames paths.hosts; - interfaces = host: outputs.nixosConfigurations.${host}.config.wol.interfaces; + hosts = lib'.nixosHosts; + interfaces = host: inputs.self.nixosConfigurations.${host}.config.wol.interfaces; in { options.wol = { diff --git a/utils/hosts.nix b/utils/hosts.nix deleted file mode 100644 index 08b0823..0000000 --- a/utils/hosts.nix +++ /dev/null @@ -1,19 +0,0 @@ -{ lib }: - -let - fs = import ./fs.nix { inherit lib; }; -in -{ - hostMeta = - hostDir: - if builtins.pathExists (hostDir + "/meta.nix") then - import (hostDir + "/meta.nix") - else - throw "meta.nix required in ${toString hostDir}"; - - adminHosts = - hostsPath: - builtins.filter (host: ((import (hostsPath + "/${host}/meta.nix")).host.admin or false)) ( - fs.dirNames hostsPath - ); -} diff --git a/utils/secrets.nix b/utils/secrets.nix deleted file mode 100644 index dab039d..0000000 --- a/utils/secrets.nix +++ /dev/null @@ -1,42 +0,0 @@ -{ lib }: - -let - mkSecrets = - sopsDir: owner: groups: - let - opts = lib.optionalAttrs (owner != null) { inherit owner; }; - mkGroup = - group: names: - let - file = "${group}.yaml"; - in - lib.foldl' lib.mergeAttrs { } ( - map (name: { - "${group}/${name}" = { - sopsFile = "${sopsDir}/${file}"; - key = name; - } - // opts; - }) names - ); - in - lib.foldl' lib.mergeAttrs { } (lib.mapAttrsToList mkGroup groups); -in -{ - mkSopsSecrets = sopsDir: mkSecrets sopsDir null; - - mkSopsUserSecrets = mkSecrets; - - sopsAvailability = - config: osConfig: - let - osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets; - hmSopsAvailable = config ? sops && config.sops ? secrets; - preferOs = osSopsAvailable; - in - { - available = osSopsAvailable || hmSopsAvailable; - secrets = if preferOs then osConfig.sops.secrets else config.sops.secrets; - templates = if preferOs then osConfig.sops.templates else config.sops.templates; - }; -}