From 3696ebd97635bd1081189d9aab0d35c47ba0c8da Mon Sep 17 00:00:00 2001 From: ahuston-0 Date: Wed, 15 May 2024 00:38:59 -0400 Subject: [PATCH] convert flake-update to a submodule type Signed-off-by: ahuston-0 --- flake.nix | 10 +++ modules/flake-update-service.nix | 135 ++++++++++++++++++------------- modules/update.nix | 7 +- utils/default.nix | 21 +++++ 4 files changed, 116 insertions(+), 57 deletions(-) create mode 100644 utils/default.nix diff --git a/flake.nix b/flake.nix index 4262039..f25fcf2 100644 --- a/flake.nix +++ b/flake.nix @@ -186,6 +186,16 @@ formatter = forEachSystem (system: nixpkgs.legacyPackages.${system}.nixfmt-rfc-style); + # adds our lib functions to lib namespace + lib = nixpkgs.lib.extend ( + self: super: { + my = import ./lib { + inherit nixpkgs inputs; + lib = self; + }; + } + ); + nixosConfigurations = let constructSystem = diff --git a/modules/flake-update-service.nix b/modules/flake-update-service.nix index 870debd..0c335e2 100644 --- a/modules/flake-update-service.nix +++ b/modules/flake-update-service.nix @@ -7,70 +7,95 @@ let cfg = config.services.autopull; + + autopull-type = lib.types.submodule { + enable = lib.mkEnableOption "autopull for ${cfg.account-name}"; + + name = lib.mkOption { + type = lib.types.str; + default = config.module._args.name; + description = "A name for the service which needs to be pulled"; + }; + + path = lib.mkOption { + type = lib.types.path; + description = "Path that needs to be updated via git pull"; + }; + + frequency = lib.mkOption { + type = lib.types.str; + description = "systemd-timer compatible time between pulls"; + default = "1h"; + }; + + ssh-key = lib.mkOption { + type = lib.types.str; + default = ""; + description = "ssh-key used to pull the repository"; + }; + + triggers-rebuild = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether or not the rebuild service should be triggered after pulling. Note that system.autoUpgrade must be pointed at the same directory as this service if you'd like to use this option."; + }; + }; in { options = { services.autopull = { enable = lib.mkEnableOption "autopull"; - name = lib.mkOption { - type = lib.types.str; - default = "dotfiles"; - description = "A name for the service which needs to be pulled"; - }; - path = lib.mkOption { - type = lib.types.nullOr lib.types.path; - default = null; - description = "Path that needs to be updated via git pull"; - }; - - frequency = lib.mkOption { - type = lib.types.str; - description = "systemd-timer compatible time between pulls"; - default = "1h"; - }; - - ssh-key = lib.mkOption { - type = lib.types.str; - default = ""; - description = "ssh-key used to pull the repository"; - }; - - triggersRebuild = lib.mkOption { - type = lib.types.bool; - default = false; - description = "Whether or not the rebuild service should be triggered after pulling. Note that system.autoUpgrade must be pointed at the same directory as this service if you'd like to use this option."; - }; + repo = lib.mkOption { type = lib.types.attrsOf autopull-type; }; }; }; - config = lib.mkIf (cfg.enable && !(builtins.isNull cfg.path)) { - environment.systemPackages = [ - pkgs.openssh - pkgs.git - ]; - systemd.services."autopull@${cfg.name}" = { - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - description = "Pull the latest data for ${cfg.name}"; - environment = lib.mkIf (cfg.ssh-key != "") { - GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${cfg.ssh-key} -o IdentitiesOnly=yes"; - }; - serviceConfig = { - Type = "oneshot"; - User = "root"; - WorkingDirectory = cfg.path; - ExecStart = "${pkgs.git}/bin/git pull --all"; - }; - }; + config = + let + repos = lib.filterAttrs (_: { enable, ... }: enable == true) cfg.repo; + in + lib.mkIf cfg.enable { + environment.systemPackages = [ + pkgs.openssh + pkgs.git + ]; + systemd.services = lib.mapAttrs' ( + repo: + { + name, + ssh-key, + triggers-rebuild, + ... + }: + lib.nameValuePair "autopull@${name}" { + requires = [ "multi-user.target" ]; + wants = lib.optionals (triggers-rebuild) [ "nixos-service.service" ]; + after = [ "multi-user.target" ]; + before = lib.optionals (triggers-rebuild) [ "nixos-upgrade.service" ]; + description = "Pull the latest data for ${name}"; + environment = lib.mkIf (ssh-key != "") { + GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${ssh-key} -o IdentitiesOnly=yes"; + }; + serviceConfig = { + Type = "oneshot"; + User = "root"; + WorkingDirectory = cfg.path; + ExecStart = "${pkgs.git}/bin/git pull --all"; + }; + } + ) repos; - systemd.timers."autopull@${cfg.name}" = { - wantedBy = [ "timers.target" ]; - timerConfig = { - OnBootSec = cfg.frequency; - OnUnitActiveSec = cfg.frequency; - Unit = "autopull@${cfg.name}.service"; - }; + systemd.timers."autopull@${cfg.name}" = lib.mapAttrs' ( + repo: + { name, frequency, ... }: + lib.nameValuePair "autopull@${name}" { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = cfg.frequency; + OnUnitActiveSec = cfg.frequency; + Unit = "autopull@${cfg.name}.service"; + }; + } + ) repos; }; - }; } diff --git a/modules/update.nix b/modules/update.nix index d88987c..3a0e630 100644 --- a/modules/update.nix +++ b/modules/update.nix @@ -2,8 +2,11 @@ { services.autopull = { enable = lib.mkDefault true; - ssh-key = lib.mkDefault "/root/.ssh/id_ed25519_ghdeploy"; - path = lib.mkDefault /root/dotfiles; + repo.dotfiles = { + enable = lib.mkDefault true; + ssh-key = lib.mkDefault "/root/.ssh/id_ed25519_ghdeploy"; + path = lib.mkDefault /root/dotfiles; + }; }; system.autoUpgrade = { diff --git a/utils/default.nix b/utils/default.nix new file mode 100644 index 0000000..819d6d5 --- /dev/null +++ b/utils/default.nix @@ -0,0 +1,21 @@ +{ + config, + lib, + pkgs, + ... +}: + +{ + # create rad-dev namespace for lib + rad-dev = { + # any(), but checks if any value in the list is true + # type: + # anyBool:: [bool] -> bool + anyBool = lib.any (n: n); + + # pulls a value out of an attrset and converts it to a list + # type: + # mapGetAttr :: String -> Attrset -> [Any] + mapGetAttr = (attr: set: lib.mapAttrsToList (_: attrset: lib.getAttr attr attrset) set); + }; +}