Add support for NixOS (#117)

You are able to use it as standalone package, or service either.
pull/120/head
Shvedov Yury 4 months ago committed by GitHub
parent 45c629ce6b
commit 5d685af85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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:

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

@ -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 ]}
''

@ -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;
}
Loading…
Cancel
Save