From 5d685af85d44415dee821034688b231555dc36e5 Mon Sep 17 00:00:00 2001 From: Shvedov Yury Date: Tue, 26 Dec 2023 16:21:22 +0300 Subject: [PATCH] Add support for NixOS (#117) You are able to use it as standalone package, or service either. --- README.md | 28 ++++++++++++++++++ flake.nix | 19 ++++++++++++ gitwatch.nix | 19 ++++++++++++ module.nix | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 flake.nix create mode 100644 gitwatch.nix create mode 100644 module.nix diff --git a/README.md b/README.md index c8ef665..c026e69 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ * [Update](#update) * [bpkg](#bpkg) * [Archlinux](#archlinux) + * [NixOs](#nixos) + * [As Module](#as-module) + * [As Package](#as-package) * [Requirements](#requirements) * [Notes for Mac](#notes-for-mac) * [What it does](#what-it-does) @@ -84,6 +87,31 @@ the command below. You may need to invoke `bpkg` with `sudo` when using the There is an [AUR](https://aur.archlinux.org/packages/gitwatch-git/) package for Archlinux. Install it with you favorite aur helper. +### NixOs + +#### As Module + +Say you add this as input `gitwatch` to your flake. Then you may want to append +field `gitwatch.modules` to your `nixosSystem` modules. Then you are able to +enable `services.gitwatch.*` service in per-repository mode. Like next: + +```nix +services.gitwatch.my-repo = { + enable = true; + path = "/home/me/my-repo"; + remote = "git@github.com:me/my-repo.git"; + user = "me"; +}; +``` + +This will make NixOS to create `systemd` service for `my-repo` repository. + +#### As Package + +You can to play around with nix package inside this repository. Call `nix run` +in this repository to run `gitwatch` script or `nix shell` to enter shell with +`gitwatch` command available. + ## Requirements To run this script, you must have installed and globally available: diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..ee2c85e --- /dev/null +++ b/flake.nix @@ -0,0 +1,19 @@ +{ + description = "A bash script to watch a file or folder and commit changes to a git repo"; + outputs = { self, nixpkgs, flake-utils }: + let + packages = flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in + { + packages = rec { + gitwatch = pkgs.callPackage ./gitwatch.sh { }; + default = gitwatch; + }; + }); + in + packages // { modules = [ ./module.nix ]; }; +} diff --git a/gitwatch.nix b/gitwatch.nix new file mode 100644 index 0000000..4205ecd --- /dev/null +++ b/gitwatch.nix @@ -0,0 +1,19 @@ +{ runCommandNoCC +, lib +, makeWrapper + +, git +, openssh +, inotify-tools +}: runCommandNoCC "gitwatch" { + nativeBuildInputs = [ makeWrapper ]; +} '' + mkdir -p $out/bin + dest="$out/bin/gitwatch" + cp ${./gitwatch.sh} $dest + chmod +x $dest + patchShebangs $dest + + wrapProgram $dest \ + --prefix PATH ';' ${lib.makeBinPath [ git inotify-tools openssh ]} +'' diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..1f3eeb5 --- /dev/null +++ b/module.nix @@ -0,0 +1,84 @@ +{ lib, pkgs, config, ... }: +let + gitwatch = pkgs.callPackage ./gitwatch.nix { }; + mkSystemdService = name: cfg: lib.nameValuePair + "gitwatch-${name}" + ( + let + getvar = flag: var: + if cfg."${var}" != null + then "${flag} ${cfg."${var}"}" + else ""; + branch = getvar "-b" "branch"; + fetcher = + if cfg.remote == null + then "true" + else '' + ''; + in + { + inherit (cfg) enable; + after = [ "network-online.target" ]; + description = "gitwatch for ${name}"; + path = with pkgs; [ gitwatch git openssh ]; + script = '' + if [ -n "${cfg.remote}" ] && ! [ -d "${cfg.path}" ]; then + git clone ${branch} "${cfg.remote}" "${cfg.path}" + fi + ${fetcher} + gitwatch ${getvar "-r" "remote"} ${branch} ${cfg.path} + ''; + serviceConfig.User = cfg.user; + } + ); +in +{ + options.services.gitwatch = lib.mkOption { + description = '' + A set of git repositories to watch for. See + [gitwatch](https://github.com/gitwatch/gitwatch) for more. + ''; + default = { }; + example = { + my-repo = { + enable = true; + user = "user"; + path = "/home/user/watched-project"; + remote = "git@github.com:me/my-project.git"; + }; + disabled-repo = { + enable = false; + user = "user"; + path = "/home/user/disabled-project"; + remote = "git@github.com:me/my-old-project.git"; + branch = "autobranch"; + }; + }; + type = with lib.types; attrsOf (submodule { + options = { + enable = lib.mkEnableOption "watching for repo"; + path = lib.mkOption { + description = "The path to repo in local machine"; + type = str; + }; + user = lib.mkOption { + description = "The name of services's user"; + type = str; + default = "root"; + }; + remote = lib.mkOption { + description = "Optional url of remote repository"; + type = nullOr str; + default = null; + }; + branch = lib.mkOption { + description = "Optional branch in remote repository"; + type = nullOr str; + default = null; + }; + }; + }); + }; + config.systemd.services = + lib.mapAttrs' mkSystemdService config.services.gitwatch; +}