{ config, lib, pkgs, ... }: with lib; let cfg = config.services.buildkite-agent; mkHookOption = { name, description, example ? null }: { inherit name; value = mkOption { default = null; inherit description; type = types.nullOr types.lines; } // (if example == null then {} else { inherit example; }); }; mkHookOptions = hooks: listToAttrs (map mkHookOption hooks); hooksDir = let mkHookEntry = name: value: '' cat > $out/${name} <services.buildkite-agent.hooks.<name> instead. ''; }; }; }; config = mkIf config.services.buildkite-agent.enable { users.extraUsers.buildkite-agent = { name = "buildkite-agent"; home = cfg.dataDir; createHome = true; description = "Buildkite agent user"; extraGroups = [ "keys" ]; }; environment.systemPackages = [ cfg.package ]; systemd.services.buildkite-agent = let copy = x: target: perms: "cp -f ${x} ${target}; ${pkgs.coreutils}/bin/chmod ${toString perms} ${target}; "; in { description = "Buildkite Agent"; wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; path = cfg.runtimePackages; environment = config.networking.proxy.envVars // { HOME = cfg.dataDir; NIX_REMOTE = "daemon"; }; ## NB: maximum care is taken so that secrets (ssh keys and the CI token) ## don't end up in the Nix store. preStart = '' ${pkgs.coreutils}/bin/mkdir -m 0700 -p ${cfg.dataDir}/.ssh ${copy (toString cfg.openssh.privateKeyPath) "${cfg.dataDir}/.ssh/id_rsa" 600} ${copy (toString cfg.openssh.publicKeyPath) "${cfg.dataDir}/.ssh/id_rsa.pub" 600} cat > "${cfg.dataDir}/buildkite-agent.cfg" <' are mutually exclusive. ''; } ]; }; imports = [ (mkRenamedOptionModule [ "services" "buildkite-agent" "token" ] [ "services" "buildkite-agent" "tokenPath" ]) (mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "privateKey" ] [ "services" "buildkite-agent" "openssh" "privateKeyPath" ]) (mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "publicKey" ] [ "services" "buildkite-agent" "openssh" "publicKeyPath" ]) ]; }