summary refs log tree commit diff
path: root/nixos/modules/virtualisation/railcar.nix
blob: e719e25650d37c33113404519b11f7a5465326a6 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.railcar;
  generateUnit = name: containerConfig:
    let
      container = pkgs.ociTools.buildContainer {
        args = [
          (pkgs.writeShellScript "run.sh" containerConfig.cmd).outPath
        ];
      };
    in
      nameValuePair "railcar-${name}" {
        enable = true;
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
            ExecStart = ''
              ${cfg.package}/bin/railcar -r ${cfg.stateDir} run ${name} -b ${container}
            '';
            Type = containerConfig.runType;
          };
      };
  mount = with types; (submodule {
    options = {
      type = mkOption {
        type = str;
        default = "none";
        description = ''
          The type of the filesystem to be mounted.
          Linux: filesystem types supported by the kernel as listed in
          `/proc/filesystems` (e.g., "minix", "ext2", "ext3", "jfs", "xfs",
          "reiserfs", "msdos", "proc", "nfs", "iso9660"). For bind mounts
          (when options include either bind or rbind), the type is a dummy,
          often "none" (not listed in /proc/filesystems).
        '';
      };
      source = mkOption {
        type = str;
        description = "Source for the in-container mount";
      };
      options = mkOption {
        type = listOf str;
        default = [ "bind" ];
        description = ''
          Mount options of the filesystem to be used.

          Support options are listed in the mount(8) man page. Note that
          both filesystem-independent and filesystem-specific options
          are listed.
        '';
      };
    };
  });
in
{
  options.services.railcar = {
    enable = mkEnableOption "railcar";

    containers = mkOption {
      default = {};
      description = "Declarative container configuration";
      type = with types; attrsOf (submodule ({ name, config, ... }: {
        options = {
          cmd = mkOption {
            type = types.lines;
            description = "Command or script to run inside the container";
          };

          mounts = mkOption {
            type = with types; attrsOf mount;
            default = {};
            description = ''
              A set of mounts inside the container.

              The defaults have been chosen for simple bindmounts, meaning
              that you only need to provide the "source" parameter.
            '';
            example = { "/data" = { source = "/var/lib/data"; }; };
          };

          runType = mkOption {
            type = types.str;
            default = "oneshot";
            description = "The systemd service run type";
          };

          os = mkOption {
            type = types.str;
            default = "linux";
            description = "OS type of the container";
          };

          arch = mkOption {
            type = types.str;
            default = "x86_64";
            description = "Computer architecture type of the container";
          };
        };
      }));
    };

    stateDir = mkOption {
      type = types.path;
      default = "/var/railcar";
      description = "Railcar persistent state directory";
    };

    package = mkOption {
      type = types.package;
      default = pkgs.railcar;
      defaultText = literalExpression "pkgs.railcar";
      description = "Railcar package to use";
    };
  };

  config = mkIf cfg.enable {
    systemd.services = flip mapAttrs' cfg.containers (name: containerConfig:
      generateUnit name containerConfig
    );
  };
}