From 1c861aa1b5eae7a5d645d5cde14a38e5966d6a74 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 29 Apr 2025 09:41:20 +0100 Subject: [PATCH] Add dev-commit Add `dev-commit`, a script based on https://github.com/ThePrimeagen/dev/blob/f00f31c649e0887343a0ec10cab6cb5553ba6639/env/.local/scripts/dev-commit that is meant to be a fallback to commit and push any missed changes that should be kept. This is not intended to be a substitute for making manual commits and writing detailed commit messages. The script uses an environment variable for the list of project directories, which is set within a Home Manager module that also creates a systemd service and timer to run the command automatically - similar to a cron job. Adding this should prevent occasional issues, such as missing the firewall configuration for my homelab which was removed accidentally and stopped my reverse proxies for services such as Jellyfin and Immich from running. --- home/opdavies/t480.nix | 15 ++++ modules/home-manager/cli/default.nix | 1 + modules/home-manager/cli/dev-commit.nix | 94 +++++++++++++++++++++++++ pkgs/default.nix | 1 + pkgs/dev-commit.nix | 26 +++++++ 5 files changed, 137 insertions(+) create mode 100644 modules/home-manager/cli/dev-commit.nix create mode 100644 pkgs/dev-commit.nix diff --git a/home/opdavies/t480.nix b/home/opdavies/t480.nix index 3dc64a32..e36cdfb8 100644 --- a/home/opdavies/t480.nix +++ b/home/opdavies/t480.nix @@ -68,6 +68,21 @@ }; }; + programs.dev-commit = { + enable = true; + + repoPaths = + let + personal = "${config.xdg.userDirs.extraConfig.XDG_REPOS_DIR}/personal"; + in + [ + "${personal}/nix-config" + "${personal}/opentofu-dns" + ]; + + schedule.enable = true; + }; + xdg.configFile."pam-gnupg".text = '' 098EE055DAD2B9CB68154C6759DD38292D2273B6 1E21B58D69FFEFAD077F152A50FEA938A3413F50 diff --git a/modules/home-manager/cli/default.nix b/modules/home-manager/cli/default.nix index 4f66690d..084c35b0 100644 --- a/modules/home-manager/cli/default.nix +++ b/modules/home-manager/cli/default.nix @@ -1,6 +1,7 @@ { imports = [ ./bluetuith.nix + ./dev-commit.nix ./direnv.nix ./fzf.nix ./git.nix diff --git a/modules/home-manager/cli/dev-commit.nix b/modules/home-manager/cli/dev-commit.nix new file mode 100644 index 00000000..9e97ab11 --- /dev/null +++ b/modules/home-manager/cli/dev-commit.nix @@ -0,0 +1,94 @@ +{ + config, + lib, + pkgs, + ... +}: + +with lib; + +let + cfg = config.programs.dev-commit; + repoPaths = concatStringsSep ":" cfg.repoPaths; +in +{ + options.programs.dev-commit = { + enable = mkEnableOption "Enable dev-commit"; + + repoPaths = mkOption { + default = [ ]; + description = "A list of repository paths that should have automated commits"; + type = types.listOf types.path; + }; + + schedule = mkOption { + type = types.submodule { + options = { + enable = mkEnableOption "Enable automated dev commits with systemd"; + + time = mkOption { + description = '' + Time expression for when to run the dev-commit job. + + This uses systemd's `OnCalendar` syntax. + + Examples: + - "hourly" (once every hour) + - "daily" (once per day at midnight) + - "Mon *-*-01 12:00:00" (every Monday at 12:00 PM) + + See `man systemd.time` for full syntax reference. + ''; + default = "hourly"; + type = types.str; + }; + }; + }; + + default = { + enable = false; + time = "hourly"; + }; + }; + }; + + config = mkIf cfg.enable { + home = { + packages = [ + pkgs.dev-commit + ]; + + sessionVariables.DEV_COMMIT_PATHS = repoPaths; + }; + + systemd.user = mkIf cfg.schedule.enable { + services.dev-commit = { + Install.WantedBy = [ "default.target" ]; + + Service = { + Environment = [ + "DEV_COMMIT_PATHS=${repoPaths}" + ]; + + ExecStart = "${lib.getExe pkgs.dev-commit}"; + + Type = "oneshot"; + }; + + Unit.Description = "dev-commit"; + }; + + timers.dev-commit = { + Install.WantedBy = [ "timers.target" ]; + + Timer = { + OnCalendar = cfg.schedule.time; + Persistent = true; + Unit = "dev-commit.service"; + }; + + Unit.Description = "Runs automated development commits in select project repositories."; + }; + }; + }; +} diff --git a/pkgs/default.nix b/pkgs/default.nix index 4b6217da..a47e9dfa 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -8,6 +8,7 @@ in { backup-websites = callPackage ./backup-websites.nix { }; build-glove80 = callPackage ./build-glove80.nix { }; + dev-commit = callPackage ./dev-commit.nix { }; displayselect = callPackage ./displayselect { }; notes = callPackage ./notes { }; passmenu-otp = callPackage ./passmenu-otp.nix { }; diff --git a/pkgs/dev-commit.nix b/pkgs/dev-commit.nix new file mode 100644 index 00000000..dfe4336a --- /dev/null +++ b/pkgs/dev-commit.nix @@ -0,0 +1,26 @@ +{ pkgs, ... }: + +pkgs.writeShellApplication { + name = "dev-commit"; + + runtimeInputs = with pkgs; [ + coreutils + git + openssh + ]; + + text = '' + IFS=':' read -ra repos <<< "$DEV_COMMIT_PATHS" + + for repo in "''${repos[@]}"; do + echo "Processing $repo" + pushd "$repo" + + git add . + git commit -m "Automated dev commit" || true + git push + + popd + done + ''; +}