Compare commits

..

No commits in common. "fb9e963b0e1746438f29b6afbcb5aeeb058e3b77" and "15c0850babc7e714ac9681944d4a0a96b48b728d" have entirely different histories.

1 changed files with 99 additions and 131 deletions

View File

@ -8,13 +8,18 @@ let
cfg = config.services.ugit; cfg = config.services.ugit;
pkg = pkgs.callPackage ./pkg.nix { inherit pkgs; }; pkg = pkgs.callPackage ./pkg.nix { inherit pkgs; };
yamlFormat = pkgs.formats.yaml { }; yamlFormat = pkgs.formats.yaml { };
instanceOptions = configFile = pkgs.writeText "ugit.yaml" (
{ name, config, ... }: builtins.readFile (yamlFormat.generate "ugit-yaml" cfg.config)
);
authorizedKeysFile = pkgs.writeText "ugit_keys" (builtins.concatStringsSep "\n" cfg.authorizedKeys);
in
{
options =
let let
inherit (lib) mkEnableOption mkOption types; inherit (lib) mkEnableOption mkOption types;
in in
{ {
options = { services.ugit = {
enable = mkEnableOption "Enable ugit"; enable = mkEnableOption "Enable ugit";
package = mkOption { package = mkOption {
@ -23,16 +28,10 @@ let
default = pkg; default = pkg;
}; };
homeDir = mkOption {
type = types.str;
description = "ugit home directory";
default = "/var/lib/ugit/${name}";
};
repoDir = mkOption { repoDir = mkOption {
type = types.str; type = types.str;
description = "where ugit stores repositories"; description = "where ugit stores repositories";
default = "/var/lib/ugit/${name}/repos"; default = "/var/lib/ugit/repos";
}; };
authorizedKeys = mkOption { authorizedKeys = mkOption {
@ -44,13 +43,13 @@ let
authorizedKeysFile = mkOption { authorizedKeysFile = mkOption {
type = types.str; type = types.str;
description = "path to authorized_keys file ugit uses for auth"; description = "path to authorized_keys file ugit uses for auth";
default = "/var/lib/ugit/${name}/authorized_keys"; default = "/var/lib/ugit/authorized_keys";
}; };
hostKeyFile = mkOption { hostKeyFile = mkOption {
type = types.str; type = types.str;
description = "path to host key file (will be created if it doesn't exist)"; description = "path to host key file (will be created if it doesn't exist)";
default = "/var/lib/ugit/${name}/ugit_ed25519"; default = "/var/lib/ugit/ugit_ed25519";
}; };
config = mkOption { config = mkOption {
@ -61,16 +60,21 @@ let
user = mkOption { user = mkOption {
type = types.str; type = types.str;
default = "ugit-${name}"; default = "ugit";
description = "User account under which ugit runs"; description = "User account under which ugit runs";
}; };
group = mkOption { group = mkOption {
type = types.str; type = types.str;
default = "ugit-${name}"; default = "ugit";
description = "Group account under which ugit runs"; description = "Group account under which ugit runs";
}; };
openFirewall = mkOption {
type = types.bool;
default = false;
};
hooks = mkOption { hooks = mkOption {
type = types.listOf ( type = types.listOf (
types.submodule { types.submodule {
@ -91,131 +95,95 @@ let
}; };
}; };
}; };
in config = lib.mkIf cfg.enable {
{ users.users."${cfg.user}" = {
options = { home = "/var/lib/ugit";
services.ugit = lib.mkOption { createHome = true;
type = lib.types.attrsOf (lib.types.submodule instanceOptions); group = "${cfg.group}";
default = { }; isSystemUser = true;
description = "Attribute set of ugit instances"; isNormalUser = false;
description = "user for ugit service";
};
users.groups."${cfg.group}" = { };
networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [
8448
8449
];
}; };
};
config = lib.mkIf (cfg != { }) {
users.users = lib.mapAttrs' (
name: instanceCfg:
lib.nameValuePair instanceCfg.user {
home = instanceCfg.homeDir;
createHome = true;
group = instanceCfg.group;
isSystemUser = true;
isNormalUser = false;
description = "user for ugit ${name} service";
}
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
users.groups = lib.mapAttrs' (name: instanceCfg: lib.nameValuePair instanceCfg.group { }) ( systemd.services = {
lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg ugit = {
); enable = true;
script =
systemd.services = lib.foldl' ( let
acc: name: authorizedKeysPath =
let if (builtins.length cfg.authorizedKeys) > 0 then authorizedKeysFile else cfg.authorizedKeysFile;
instanceCfg = cfg.${name}; args = [
in "--config=${configFile}"
lib.recursiveUpdate acc ( "--repo-dir=${cfg.repoDir}"
lib.optionalAttrs instanceCfg.enable { "--ssh.authorized-keys=${authorizedKeysPath}"
"ugit-${name}" = { "--ssh.host-key=${cfg.hostKeyFile}"
enable = true;
description = "ugit instance ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
path = [
instanceCfg.package
pkgs.git
pkgs.bash
]; ];
serviceConfig = { in
User = instanceCfg.user; "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
Group = instanceCfg.group; wantedBy = [ "multi-user.target" ];
Restart = "always"; after = [ "network.target" ];
RestartSec = "15"; path = [
WorkingDirectory = instanceCfg.homeDir; cfg.package
ExecStart = pkgs.git
let pkgs.bash
configFile = pkgs.writeText "ugit-${name}.yaml" ( ];
builtins.readFile (yamlFormat.generate "ugit-${name}-yaml" instanceCfg.config) serviceConfig = {
); User = cfg.user;
authorizedKeysFile = pkgs.writeText "ugit_${name}_keys" ( Group = cfg.group;
builtins.concatStringsSep "\n" instanceCfg.authorizedKeys Restart = "always";
); RestartSec = "15";
WorkingDirectory = "/var/lib/ugit";
authorizedKeysPath = };
if (builtins.length instanceCfg.authorizedKeys) > 0 then };
authorizedKeysFile ugit-hooks = {
else wantedBy = [ "multi-user.target" ];
instanceCfg.authorizedKeysFile; after = [ "ugit.service" ];
args = [ requires = [ "ugit.service" ];
"--config=${configFile}" serviceConfig = {
"--repo-dir=${instanceCfg.repoDir}" Type = "oneshot";
"--ssh.authorized-keys=${authorizedKeysPath}" ExecStart =
"--ssh.host-key=${instanceCfg.hostKeyFile}" let
]; script = pkgs.writeShellScript "ugit-hooks-link" (
in builtins.concatStringsSep "\n" (
"${instanceCfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}"; map (
};
};
"ugit-${name}-hooks" = {
description = "Setup hooks for ugit instance ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "ugit-${name}.service" ];
requires = [ "ugit-${name}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
User = instanceCfg.user;
Group = instanceCfg.group;
ExecStart =
let
hookDir = "${instanceCfg.repoDir}/hooks/pre-receive.d";
mkHookScript =
hook: hook:
let let
script = pkgs.writeShellScript "ugit-${name}-${hook.name}" hook.content; script = pkgs.writeShellScript hook.name hook.content;
path = "${cfg.repoDir}/hooks/pre-receive.d/${hook.name}";
in in
'' "ln -s ${script} ${path}"
mkdir -p ${hookDir} ) cfg.hooks
ln -sf ${script} ${hookDir}/${hook.name} )
''; );
in in
pkgs.writeShellScript "ugit-${name}-hooks-setup" '' "${script}";
${builtins.concatStringsSep "\n" (map mkHookScript instanceCfg.hooks)} };
''; };
};
systemd.tmpfiles.settings.ugit = builtins.listToAttrs (
map (
hook:
let
script = pkgs.writeShellScript hook.name hook.content;
path = "${cfg.repoDir}/hooks/pre-receive.d/${hook.name}";
in
{
name = path;
value = {
"L" = {
argument = "${script}";
}; };
}; };
} }
) ) cfg.hooks
) { } (builtins.attrNames cfg); );
systemd.tmpfiles.settings = lib.mapAttrs' (
name: instanceCfg:
builtins.listToAttrs (
map (
hook:
let
script = pkgs.writeShellScript hook.name hook.content;
path = "${instanceCfg.repoDir}/hooks/pre-receive.d/${hook.name}";
in
{
name = path;
value = {
"L" = {
argument = "${script}";
};
};
}
) instanceCfg.hooks
)
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
}; };
} }