summaryrefslogtreecommitdiff
path: root/modules/machine-id/default.nix
blob: 2c33db266ad504d57a1b9a5813152989e0f7350e (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
{ config, pkgs, lib, hostName, ... }:

with lib;

let
  cfg = config.system.machine-id.generate;

  generate = pkgs.writers.writePython3 "generate" {} ''
    from hashlib import blake2s
    from sys import argv
    from base64 import b64decode


    print(blake2s(
        argv[2].strip().encode('utf-8'),
        key=b64decode(argv[1]),
        person=b'machn-id',
        digest_size=16,
    ).hexdigest())
  '';

  machine-id = trim (builtins.readFile (pkgs.runCommand "machine-id" { inherit hostName; } ''
    ${generate} "${builtins.readFile ./seed}" "$hostName" > $out
  ''));
in {
  options = {
    system.machine-id.generate = {
      enable = mkEnableOption "automatic generation of `/etc/machine-id`" // { default = true; };
    };
  };

  config = mkIf cfg.enable {
    environment.etc."machine-id".text = mkDefault machine-id;
    networking.hostId = mkDefault (substring 0 8 machine-id);
  };
}