summaryrefslogtreecommitdiffstats
path: root/nixos/modules/virtualisation/digital-ocean-image.nix
blob: 3d9fe52bb0db7e11164a84360aab29568200bd97 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;
let
  cfg = config.virtualisation.digitalOceanImage;
in
{

  imports = [
    ./digital-ocean-config.nix
    ./disk-size-option.nix
    ../image/file-options.nix
    (lib.mkRenamedOptionModuleWith {
      sinceRelease = 2411;
      from = [
        "virtualisation"
        "digitalOceanImage"
        "diskSize"
      ];
      to = [
        "virtualisation"
        "diskSize"
      ];
    })
  ];

  options = {
    virtualisation.digitalOceanImage.configFile = mkOption {
      type = with types; nullOr path;
      default = null;
      description = ''
        A path to a configuration file which will be placed at
        `/etc/nixos/configuration.nix` and be used when switching
        to a new configuration. If set to `null`, a default
        configuration is used that imports
        `(modulesPath + "/virtualisation/digital-ocean-config.nix")`.
      '';
    };

    virtualisation.digitalOceanImage.compressionMethod = mkOption {
      type = types.enum [
        "gzip"
        "bzip2"
      ];
      default = "gzip";
      example = "bzip2";
      description = ''
        Disk image compression method. Choose bzip2 to generate smaller images that
        take longer to generate but will consume less metered storage space on your
        Digital Ocean account.
      '';
    };
  };

  #### implementation
  config =
    let
      format = "qcow2";
    in
    {
      image.extension = lib.concatStringsSep "." [
        format
        (
          {
            "gzip" = "gz";
            "bzip2" = "bz2";
          }
          .${cfg.compressionMethod}
        )
      ];
      system.nixos.tags = [ "digital-ocean" ];
      system.build.image = config.system.build.digitalOceanImage;
      system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
        name = "digital-ocean-image";
        inherit (config.image) baseName;
        inherit (config.virtualisation) diskSize;
        inherit
          config
          lib
          pkgs
          format
          ;
        postVM =
          let
            compress =
              {
                "gzip" = "${pkgs.gzip}/bin/gzip";
                "bzip2" = "${pkgs.bzip2}/bin/bzip2";
              }
              .${cfg.compressionMethod};
          in
          ''
            ${compress} $diskImage
          '';
        configFile =
          if cfg.configFile == null then
            config.virtualisation.digitalOcean.defaultConfigFile
          else
            cfg.configFile;
      };

    };

  meta.maintainers = with maintainers; [
    arianvp
    eamsden
  ];

}