mirror of https://git.jolheiser.com/ugit.git
multiple nix module instances
parent
15c0850bab
commit
4d8423b4b3
|
@ -8,18 +8,12 @@ let
|
|||
cfg = config.services.ugit;
|
||||
pkg = pkgs.callPackage ./pkg.nix { inherit pkgs; };
|
||||
yamlFormat = pkgs.formats.yaml { };
|
||||
configFile = pkgs.writeText "ugit.yaml" (
|
||||
builtins.readFile (yamlFormat.generate "ugit-yaml" cfg.config)
|
||||
);
|
||||
authorizedKeysFile = pkgs.writeText "ugit_keys" (builtins.concatStringsSep "\n" cfg.authorizedKeys);
|
||||
in
|
||||
{
|
||||
options =
|
||||
instanceOptions =
|
||||
let
|
||||
inherit (lib) mkEnableOption mkOption types;
|
||||
in
|
||||
{
|
||||
services.ugit = {
|
||||
options = {
|
||||
enable = mkEnableOption "Enable ugit";
|
||||
|
||||
package = mkOption {
|
||||
|
@ -28,6 +22,12 @@ in
|
|||
default = pkg;
|
||||
};
|
||||
|
||||
homeDir = mkOption {
|
||||
type = types.str;
|
||||
description = "ugit home directory";
|
||||
default = "/var/lib/ugit";
|
||||
};
|
||||
|
||||
repoDir = mkOption {
|
||||
type = types.str;
|
||||
description = "where ugit stores repositories";
|
||||
|
@ -70,11 +70,6 @@ in
|
|||
description = "Group account under which ugit runs";
|
||||
};
|
||||
|
||||
openFirewall = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
|
||||
hooks = mkOption {
|
||||
type = types.listOf (
|
||||
types.submodule {
|
||||
|
@ -95,51 +90,74 @@ in
|
|||
};
|
||||
};
|
||||
};
|
||||
config = lib.mkIf cfg.enable {
|
||||
users.users."${cfg.user}" = {
|
||||
home = "/var/lib/ugit";
|
||||
in
|
||||
{
|
||||
options = {
|
||||
services.ugit = lib.mkOption {
|
||||
type = lib.types.attrsOf (lib.types.submodule instanceOptions);
|
||||
default = { };
|
||||
description = "Attribute set of ugit instances";
|
||||
};
|
||||
};
|
||||
config = lib.mkIf (cfg != { }) {
|
||||
users.users = lib.mapAttrs' (
|
||||
name: instanceCfg:
|
||||
lib.nameValuePair instanceCfg.user {
|
||||
home = instanceCfg.homeDir;
|
||||
createHome = true;
|
||||
group = "${cfg.group}";
|
||||
group = instanceCfg.group;
|
||||
isSystemUser = true;
|
||||
isNormalUser = false;
|
||||
description = "user for ugit service";
|
||||
};
|
||||
users.groups."${cfg.group}" = { };
|
||||
networking.firewall = lib.mkIf cfg.openFirewall {
|
||||
allowedTCPPorts = [
|
||||
8448
|
||||
8449
|
||||
];
|
||||
};
|
||||
description = "user for ugit ${name} service";
|
||||
}
|
||||
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
|
||||
|
||||
systemd.services = {
|
||||
ugit = {
|
||||
users.groups = lib.mapAttrs' (name: instanceCfg: lib.nameValuePair instanceCfg.group { }) (
|
||||
lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg
|
||||
);
|
||||
|
||||
systemd.services = lib.mapAttrs' (
|
||||
name: instanceCfg:
|
||||
lib.nameValuePair "ugit-${name}" {
|
||||
ugit =
|
||||
let
|
||||
configFile = pkgs.writeText "ugit.yaml" (
|
||||
builtins.readFile (yamlFormat.generate "ugit-yaml" instanceCfg.config)
|
||||
);
|
||||
authorizedKeysFile = pkgs.writeText "ugit_keys" (
|
||||
builtins.concatStringsSep "\n" instanceCfg.authorizedKeys
|
||||
);
|
||||
in
|
||||
{
|
||||
enable = true;
|
||||
script =
|
||||
let
|
||||
authorizedKeysPath =
|
||||
if (builtins.length cfg.authorizedKeys) > 0 then authorizedKeysFile else cfg.authorizedKeysFile;
|
||||
if (builtins.length instanceCfg.authorizedKeys) > 0 then
|
||||
authorizedKeysFile
|
||||
else
|
||||
instanceCfg.authorizedKeysFile;
|
||||
args = [
|
||||
"--config=${configFile}"
|
||||
"--repo-dir=${cfg.repoDir}"
|
||||
"--repo-dir=${instanceCfg.repoDir}"
|
||||
"--ssh.authorized-keys=${authorizedKeysPath}"
|
||||
"--ssh.host-key=${cfg.hostKeyFile}"
|
||||
"--ssh.host-key=${instanceCfg.hostKeyFile}"
|
||||
];
|
||||
in
|
||||
"${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
|
||||
"${instanceCfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network.target" ];
|
||||
path = [
|
||||
cfg.package
|
||||
instanceCfg.package
|
||||
pkgs.git
|
||||
pkgs.bash
|
||||
];
|
||||
serviceConfig = {
|
||||
User = cfg.user;
|
||||
Group = cfg.group;
|
||||
User = instanceCfg.user;
|
||||
Group = instanceCfg.group;
|
||||
Restart = "always";
|
||||
RestartSec = "15";
|
||||
WorkingDirectory = "/var/lib/ugit";
|
||||
WorkingDirectory = instanceCfg.homeDir;
|
||||
};
|
||||
};
|
||||
ugit-hooks = {
|
||||
|
@ -156,17 +174,18 @@ in
|
|||
hook:
|
||||
let
|
||||
script = pkgs.writeShellScript hook.name hook.content;
|
||||
path = "${cfg.repoDir}/hooks/pre-receive.d/${hook.name}";
|
||||
path = "${instanceCfg.repoDir}/hooks/pre-receive.d/${hook.name}";
|
||||
in
|
||||
"ln -s ${script} ${path}"
|
||||
) cfg.hooks
|
||||
) instanceCfg.hooks
|
||||
)
|
||||
);
|
||||
in
|
||||
"${script}";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);
|
||||
|
||||
systemd.tmpfiles.settings.ugit = builtins.listToAttrs (
|
||||
map (
|
||||
|
|
Loading…
Reference in New Issue