2017-02-13 04:01:28 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
2023-10-17 21:01:15 +00:00
|
|
|
cfg = config.services.amazon-ssm-agent;
|
2017-02-13 04:01:28 +00:00
|
|
|
|
|
|
|
# The SSM agent doesn't pay attention to our /etc/os-release yet, and the lsb-release tool
|
|
|
|
# in nixpkgs doesn't seem to work properly on NixOS, so let's just fake the two fields SSM
|
|
|
|
# looks for. See https://github.com/aws/amazon-ssm-agent/issues/38 for upstream fix.
|
|
|
|
fake-lsb-release = pkgs.writeScriptBin "lsb_release" ''
|
2018-03-01 19:38:53 +00:00
|
|
|
#!${pkgs.runtimeShell}
|
2017-02-13 04:01:28 +00:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
-i) echo "nixos";;
|
2017-04-01 00:00:00 +00:00
|
|
|
-r) echo "${config.system.nixos.version}";;
|
2017-02-13 04:01:28 +00:00
|
|
|
esac
|
|
|
|
'';
|
2023-10-22 19:55:05 +00:00
|
|
|
|
|
|
|
sudoRule = {
|
|
|
|
users = [ "ssm-user" ];
|
|
|
|
commands = [ { command = "ALL"; options = [ "NOPASSWD" ]; } ];
|
|
|
|
};
|
2017-02-13 04:01:28 +00:00
|
|
|
in {
|
2023-10-17 21:01:15 +00:00
|
|
|
imports = [
|
2024-08-24 20:05:38 +00:00
|
|
|
(lib.mkRenamedOptionModule [ "services" "ssm-agent" "enable" ] [ "services" "amazon-ssm-agent" "enable" ])
|
|
|
|
(lib.mkRenamedOptionModule [ "services" "ssm-agent" "package" ] [ "services" "amazon-ssm-agent" "package" ])
|
2023-10-17 21:01:15 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
options.services.amazon-ssm-agent = {
|
2024-08-24 20:05:38 +00:00
|
|
|
enable = lib.mkEnableOption "Amazon SSM agent";
|
|
|
|
package = lib.mkPackageOption pkgs "amazon-ssm-agent" {};
|
2017-02-13 04:01:28 +00:00
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:38 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2023-10-25 09:29:27 +00:00
|
|
|
# See https://github.com/aws/amazon-ssm-agent/blob/mainline/packaging/linux/amazon-ssm-agent.service
|
2023-10-17 21:01:15 +00:00
|
|
|
systemd.services.amazon-ssm-agent = {
|
2017-02-13 04:01:28 +00:00
|
|
|
inherit (cfg.package.meta) description;
|
2023-10-04 06:23:30 +00:00
|
|
|
wants = [ "network-online.target" ];
|
2023-10-25 09:29:27 +00:00
|
|
|
after = [ "network-online.target" ];
|
2017-02-13 04:01:28 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2024-09-19 03:18:28 +00:00
|
|
|
path = [
|
|
|
|
fake-lsb-release
|
|
|
|
pkgs.coreutils
|
|
|
|
"/run/wrappers"
|
|
|
|
"/run/current-system/sw"
|
|
|
|
];
|
2023-10-25 09:29:27 +00:00
|
|
|
|
2017-02-13 04:01:28 +00:00
|
|
|
serviceConfig = {
|
2020-06-26 10:15:08 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/amazon-ssm-agent";
|
2017-02-13 04:01:28 +00:00
|
|
|
KillMode = "process";
|
2021-05-08 23:03:38 +00:00
|
|
|
# We want this restating pretty frequently. It could be our only means
|
|
|
|
# of accessing the instance.
|
|
|
|
Restart = "always";
|
2023-10-25 09:29:27 +00:00
|
|
|
RestartPreventExitStatus = 194;
|
|
|
|
RestartSec = "90";
|
2017-02-13 04:01:28 +00:00
|
|
|
};
|
|
|
|
};
|
2020-09-28 23:15:36 +00:00
|
|
|
|
|
|
|
# Add user that Session Manager needs, and give it sudo.
|
|
|
|
# This is consistent with Amazon Linux 2 images.
|
2023-10-22 19:55:05 +00:00
|
|
|
security.sudo.extraRules = [ sudoRule ];
|
|
|
|
security.sudo-rs.extraRules = [ sudoRule ];
|
|
|
|
|
2020-09-28 23:15:36 +00:00
|
|
|
# On Amazon Linux 2 images, the ssm-user user is pretty much a
|
|
|
|
# normal user with its own group. We do the same.
|
|
|
|
users.groups.ssm-user = {};
|
|
|
|
users.users.ssm-user = {
|
|
|
|
isNormalUser = true;
|
|
|
|
group = "ssm-user";
|
|
|
|
};
|
2021-05-08 23:03:38 +00:00
|
|
|
|
2023-10-25 09:29:27 +00:00
|
|
|
environment.etc."amazon/ssm/seelog.xml".source = "${cfg.package}/etc/amazon/ssm/seelog.xml.template";
|
2021-05-08 23:03:38 +00:00
|
|
|
|
|
|
|
environment.etc."amazon/ssm/amazon-ssm-agent.json".source = "${cfg.package}/etc/amazon/ssm/amazon-ssm-agent.json.template";
|
|
|
|
|
2017-02-13 04:01:28 +00:00
|
|
|
};
|
|
|
|
}
|