Skip to content

Instantly share code, notes, and snippets.

@aforemny
Created March 14, 2013 14:22
Show Gist options
  • Select an option

  • Save aforemny/5161719 to your computer and use it in GitHub Desktop.

Select an option

Save aforemny/5161719 to your computer and use it in GitHub Desktop.
Git repository management
{ config, pkgs, ... }:
with pkgs.lib;
let
mainCfg = config.services.git;
repositoryOpts = { name, config, ... }: {
options = {
name = mkOption {
type = with types; uniq string;
description = "";
};
users = mkOption {
type = with types; listOf string;
default = [];
description = "";
};
};
config = {
name = mkDefault name;
};
};
repositories = concatStrings (map (r: "${r.name} ")
(attrValues mainCfg.repositories));
in {
options.services.git = {
enable = mkOption {
default = false;
description = "";
};
user = mkOption {
default = "git";
description = "";
};
group = mkOption {
default = "git";
description = "";
};
repositories = mkOption {
default = {};
type = types.loaOf types.optionSet;
description = "";
options = repositoryOpts;
};
};
config = mkIf config.services.git.enable {
environment.systemPackages = [ pkgs.git ];
users.extraUsers = optionalAttrs (mainCfg.user == "git")
(singleton { name = mainCfg.user; description = "Git user"; })
++ concatMap (r: map (u: { name = u; extraGroups = singleton "git-${r.name}"; }) r.users)
(attrValues mainCfg.repositories);
users.extraGroups = optionalAttrs (mainCfg.group == "git")
(singleton { name = "git"; })
++ map (r: { name = "git-${r.name}"; })
(attrValues mainCfg.repositories);
jobs.git = {
startOn = "started network-interfaces";
stopOn = "stopped network-interfaces";
preStart = ''
mkdir -m 750 -p /var/srv/git
chown ${mainCfg.user}:${mainCfg.group} /var/srv/git
for fn in ${repositories}; do
mkdir -m 750 -p "/var/srv/git/$fn"
chown ${mainCfg.user}:${mainCfg.group} "/var/srv/git/$fn"
done
'';
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment