@@ -0,0 +1,153 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.flannel;
7+
8+ networkConfig = filterAttrs (n: v: v != null) {
9+ Network = cfg.network;
10+ SubnetLen = cfg.subnetLen;
11+ SubnetMin = cfg.subnetMin;
12+ SubnetMax = cfg.subnetMax;
13+ Backend = cfg.backend;
14+ };
15+in {
16+ options.services.flannel = {
17+ enable = mkEnableOption "flannel";
18+
19+ package = mkOption {
20+ description = "Package to use for flannel";
21+ type = types.package;
22+ default = pkgs.flannel.bin;
23+ };
24+
25+ publicIp = mkOption {
26+ description = ''
27+ IP accessible by other nodes for inter-host communication.
28+ Defaults to the IP of the interface being used for communication.
29+ '';
30+ type = types.nullOr types.str;
31+ default = null;
32+ };
33+
34+ iface = mkOption {
35+ description = ''
36+ Interface to use (IP or name) for inter-host communication.
37+ Defaults to the interface for the default route on the machine.
38+ '';
39+ type = types.nullOr types.str;
40+ default = null;
41+ };
42+
43+ etcd = {
44+ endpoints = mkOption {
45+ description = "Etcd endpoints";
46+ type = types.listOf types.str;
47+ default = ["http://127.0.0.1:2379"];
48+ };
49+
50+ prefix = mkOption {
51+ description = "Etcd key prefix";
52+ type = types.str;
53+ default = "/coreos.com/network";
54+ };
55+
56+ caFile = mkOption {
57+ description = "Etcd certificate authority file";
58+ type = types.nullOr types.path;
59+ default = null;
60+ };
61+
62+ certFile = mkOption {
63+ description = "Etcd cert file";
64+ type = types.nullOr types.path;
65+ default = null;
66+ };
67+
68+ keyFile = mkOption {
69+ description = "Etcd key file";
70+ type = types.nullOr types.path;
71+ default = null;
72+ };
73+ };
74+
75+ network = mkOption {
76+ description = " IPv4 network in CIDR format to use for the entire flannel network.";
77+ type = types.str;
78+ };
79+
80+ subnetLen = mkOption {
81+ description = ''
82+ The size of the subnet allocated to each host. Defaults to 24 (i.e. /24)
83+ unless the Network was configured to be smaller than a /24 in which case
84+ it is one less than the network.
85+ '';
86+ type = types.int;
87+ default = 24;
88+ };
89+
90+ subnetMin = mkOption {
91+ description = ''
92+ The beginning of IP range which the subnet allocation should start with.
93+ Defaults to the first subnet of Network.
94+ '';
95+ type = types.nullOr types.str;
96+ default = null;
97+ };
98+
99+ subnetMax = mkOption {
100+ description = ''
101+ The end of IP range which the subnet allocation should start with.
102+ Defaults to the last subnet of Network.
103+ '';
104+ type = types.nullOr types.str;
105+ default = null;
106+ };
107+
108+ backend = mkOption {
109+ description = "Type of backend to use and specific configurations for that backend.";
110+ type = types.attrs;
111+ default = {
112+ Type = "vxlan";
113+ };
114+ };
115+ };
116+
117+ config = mkIf cfg.enable {
118+ systemd.services.flannel = {
119+ description = "Flannel Service";
120+ wantedBy = [ "multi-user.target" ];
121+ after = [ "network.target" ];
122+ environment = {
123+ FLANNELD_PUBLIC_IP = cfg.publicIp;
124+ FLANNELD_ETCD_ENDPOINTS = concatStringsSep "," cfg.etcd.endpoints;
125+ FLANNELD_ETCD_KEYFILE = cfg.etcd.keyFile;
126+ FLANNELD_ETCD_CERTFILE = cfg.etcd.certFile;
127+ FLANNELD_ETCD_CAFILE = cfg.etcd.caFile;
128+ FLANNELD_IFACE = cfg.iface;
129+ ETCDCTL_CERT_FILE = cfg.etcd.certFile;
130+ ETCDCTL_KEY_FILE = cfg.etcd.keyFile;
131+ ETCDCTL_CA_FILE = cfg.etcd.caFile;
132+ ETCDCTL_PEERS = concatStringsSep "," cfg.etcd.endpoints;
133+ };
134+ preStart = ''
135+ echo "setting network configuration"
136+ until ${pkgs.etcdctl.bin}/bin/etcdctl set /coreos.com/network/config '${builtins.toJSON networkConfig}'
137+ do
138+ echo "setting network configuration, retry"
139+ sleep 1
140+ done
141+ '';
142+ postStart = ''
143+ while [ ! -f /run/flannel/subnet.env ]
144+ do
145+ sleep 1
146+ done
147+ '';
148+ serviceConfig.ExecStart = "${cfg.package}/bin/flannel";
149+ };
150+
151+ services.etcd.enable = mkDefault cfg.etcd.endpoints == ["http://127.0.0.1:2379"];
152+ };
153+}