summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/announce-activation.nix
blob: 76eb4b13620c7c36d5744e7d34e3344bb4b5bad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
with import <stockholm/lib>;
{ config, pkgs, ... }: let
  cfg = config.krebs.announce-activation;
  announce-activation = pkgs.writeDash "announce-activation" ''
    set -efu
    message=$(${cfg.get-message})
    exec ${pkgs.irc-announce}/bin/irc-announce \
        ${shell.escape cfg.irc.server} \
        ${shell.escape (toString cfg.irc.port)} \
        ${shell.escape cfg.irc.nick} \
        ${shell.escape cfg.irc.channel} \
        "$message"
  '';
  default-get-message = pkgs.writeDash "announce-activation-get-message" ''
    set -efu
    PATH=${makeBinPath [
      pkgs.coreutils
      pkgs.gawk
      pkgs.gnused
      pkgs.nix
    ]}
    profile=/nix/var/nix/profiles/system
    gen_info=$(nix-env -p "$profile" --list-generations | tail -1)
    gen_no=$(echo "$gen_info" | awk '{print$1}')
    pretty_name=$(sed -n '/^PRETTY_NAME=/{s/.*="//;s/"$//;p}' /etc/os-release)
    echo "activating generation $gen_no $pretty_name"
  '';
in {
  options.krebs.announce-activation = {
    enable = mkEnableOption "announce-activation";
    get-message = mkOption {
      default = default-get-message;
      type = types.package;
    };
    irc = {
      # TODO rename channel to target?
      channel = mkOption {
        default = "#xxx";
        type = types.str; # TODO types.irc-channel
      };
      nick = mkOption {
        default = config.krebs.build.host.name;
        type = types.label;
      };
      port = mkOption {
        default = 6667;
        type = types.int;
      };
      server = mkOption {
        default = "irc.r";
        type = types.hostname;
      };
    };
  };
  config = mkIf cfg.enable {
    system.activationScripts.announce-activation = stringAfter [ "etc" ] ''
      ${announce-activation}
    '';
  };
}