summaryrefslogtreecommitdiffstats
path: root/nixos/modules/virtualisation/oci-image.nix
blob: 1856b16f3d175c430ad8a0d159002743ff00fd07 (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
61
62
63
64
65
66
67
68
69
70
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.oci;
in
{
  imports = [
    ./oci-common.nix
    ../image/file-options.nix
  ];

  config = {
    # Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
    # to avoid breaking existing configs using that.
    virtualisation.diskSize = lib.mkOverride 1490 (8 * 1024);
    virtualisation.diskSizeAutoSupported = false;

    system.nixos.tags = [ "oci" ];
    image.extension = "qcow2";
    system.build.image = config.system.build.OCIImage;
    system.build.OCIImage = import ../../lib/make-disk-image.nix {
      inherit config lib pkgs;
      inherit (config.virtualisation) diskSize;
      name = "oci-image";
      baseName = config.image.baseName;
      configFile = ./oci-config-user.nix;
      format = "qcow2";
      partitionTableType = if cfg.efi then "efi" else "legacy";
    };

    systemd.services.fetch-ssh-keys = {
      description = "Fetch authorized_keys for root user";

      wantedBy = [ "sshd.service" ];
      before = [ "sshd.service" ];

      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];

      path = [
        pkgs.coreutils
        pkgs.curl
      ];
      script = ''
        mkdir -m 0700 -p /root/.ssh
        if [ -f /root/.ssh/authorized_keys ]; then
          echo "Authorized keys have already been downloaded"
        else
          echo "Downloading authorized keys from Instance Metadata Service v2"
          curl -s -S -L \
            -H "Authorization: Bearer Oracle" \
            -o /root/.ssh/authorized_keys \
            http://169.254.169.254/opc/v2/instance/metadata/ssh_authorized_keys
          chmod 600 /root/.ssh/authorized_keys
        fi
      '';
      serviceConfig = {
        Type = "oneshot";
        RemainAfterExit = true;
        StandardError = "journal+console";
        StandardOutput = "journal+console";
      };
    };
  };
}