summary refs log tree commit diff
path: root/fleet/pkgs/declarative-git-repository/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'fleet/pkgs/declarative-git-repository/default.nix')
-rw-r--r--fleet/pkgs/declarative-git-repository/default.nix53
1 files changed, 53 insertions, 0 deletions
diff --git a/fleet/pkgs/declarative-git-repository/default.nix b/fleet/pkgs/declarative-git-repository/default.nix
new file mode 100644
index 0000000..f3bb014
--- /dev/null
+++ b/fleet/pkgs/declarative-git-repository/default.nix
@@ -0,0 +1,53 @@
+# SPDX-FileCopyrightText: V <v@unfathomable.blue>
+# SPDX-License-Identifier: OSL-3.0
+
+{ lib, writeTextDir, writeText, buildEnv, writeTextFile, bash, writeScript }:
+
+{ path
+, branch ? "trunk"
+, description ? "Unnamed repository; edit this file 'description' to name the repository."
+, config ? {}
+, hooks ? {}
+, user ? "-", group ? "-"
+}:
+
+with lib;
+
+let
+  # As generated by an initial `git init --bare`
+  defaultConfig = {
+    core = {
+      repositoryFormatVersion = 0;
+      fileMode = true;
+      bare = true;
+    };
+  };
+
+  hooksDir = buildEnv {
+    name = "git-repository-hooks";
+    paths = mapAttrsToList (hook: scripts: writeTextFile {
+      name = hook;
+      text = ''
+        #! ${bash}/bin/bash -e
+      '' + concatMapStrings (script: ''
+        ${script} "$@"
+      '') scripts;
+      destination = "/${hook}";
+      executable = true;
+    }) hooks;
+  };
+in writeTextDir "lib/tmpfiles.d/git-repository${replaceStrings [ "/" ] [ "-" ] path}.conf" ''
+  # Root directory needs the correct permissions
+  d ${path} - ${user} ${group}
+
+  # This is the smallest set of paths that Git will still recognise as a valid repository.
+  # Everything else will be automatically filled out after a push or pull.
+  f+ ${path}/HEAD - ${user} ${group} - ref: refs/heads/${branch}
+  d ${path}/objects - ${user} ${group}
+  d ${path}/refs - ${user} ${group}
+
+  # Extra stuff we want to use
+  L+ ${path}/config - - - -  ${writeText "git-repository-config" (generators.toGitINI (recursiveUpdate defaultConfig config))}
+  L+ ${path}/description - - - - ${builtins.toFile "git-repository-description" description}
+  L+ ${path}/hooks - - - - ${hooksDir}
+''