convert flake-update to a submodule type

Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
ahuston-0 2024-05-15 00:38:59 -04:00 committed by Alice Huston
parent 3fb96c2248
commit 3696ebd976
4 changed files with 116 additions and 57 deletions

View File

@ -186,6 +186,16 @@
formatter = forEachSystem (system: nixpkgs.legacyPackages.${system}.nixfmt-rfc-style); 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 = nixosConfigurations =
let let
constructSystem = constructSystem =

View File

@ -7,70 +7,95 @@
let let
cfg = config.services.autopull; 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 in
{ {
options = { options = {
services.autopull = { services.autopull = {
enable = lib.mkEnableOption "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 { repo = lib.mkOption { type = lib.types.attrsOf autopull-type; };
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.";
};
}; };
}; };
config = lib.mkIf (cfg.enable && !(builtins.isNull cfg.path)) { config =
environment.systemPackages = [ let
pkgs.openssh repos = lib.filterAttrs (_: { enable, ... }: enable == true) cfg.repo;
pkgs.git in
]; lib.mkIf cfg.enable {
systemd.services."autopull@${cfg.name}" = { environment.systemPackages = [
wantedBy = [ "multi-user.target" ]; pkgs.openssh
after = [ "network.target" ]; pkgs.git
description = "Pull the latest data for ${cfg.name}"; ];
environment = lib.mkIf (cfg.ssh-key != "") { systemd.services = lib.mapAttrs' (
GIT_SSH_COMMAND = "${pkgs.openssh}/bin/ssh -i ${cfg.ssh-key} -o IdentitiesOnly=yes"; repo:
}; {
serviceConfig = { name,
Type = "oneshot"; ssh-key,
User = "root"; triggers-rebuild,
WorkingDirectory = cfg.path; ...
ExecStart = "${pkgs.git}/bin/git pull --all"; }:
}; 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}" = { systemd.timers."autopull@${cfg.name}" = lib.mapAttrs' (
wantedBy = [ "timers.target" ]; repo:
timerConfig = { { name, frequency, ... }:
OnBootSec = cfg.frequency; lib.nameValuePair "autopull@${name}" {
OnUnitActiveSec = cfg.frequency; wantedBy = [ "timers.target" ];
Unit = "autopull@${cfg.name}.service"; timerConfig = {
}; OnBootSec = cfg.frequency;
OnUnitActiveSec = cfg.frequency;
Unit = "autopull@${cfg.name}.service";
};
}
) repos;
}; };
};
} }

View File

@ -2,8 +2,11 @@
{ {
services.autopull = { services.autopull = {
enable = lib.mkDefault true; enable = lib.mkDefault true;
ssh-key = lib.mkDefault "/root/.ssh/id_ed25519_ghdeploy"; repo.dotfiles = {
path = lib.mkDefault /root/dotfiles; enable = lib.mkDefault true;
ssh-key = lib.mkDefault "/root/.ssh/id_ed25519_ghdeploy";
path = lib.mkDefault /root/dotfiles;
};
}; };
system.autoUpgrade = { system.autoUpgrade = {

21
utils/default.nix Normal file
View File

@ -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);
};
}