-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
138 lines (123 loc) · 4.58 KB
/
flake.nix
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{
description = "Cloudflare DNS IP updater";
# Nixpkgs / NixOS version to use.
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = { self, nixpkgs }:
let
# to work with older version of flakes
lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
# System types to support.
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
in
rec {
# Provide some binary packages for selected system types.
packages = forAllSystems
(system:
let
pkgs = nixpkgsFor.${system};
in
{
cloudflare-dns-ip = pkgs.buildGo122Module rec {
noCheck = true;
pname = "cloudflare-dns-ip";
version = "v0.1";
src = ./.;
subPackages = [
"cmd/cli"
];
postInstall = ''
mv $out/bin/{cli,${pname}}
'';
checkPhase = false;
vendorHash = "sha256-JFvC9V0xS8SZSdLsOtpyTrFzXjYAOaPQaJHdcnJzK3s=";
};
}
);
# Add dependencies that are only needed for development
devShells = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
baseDeps = with pkgs; [ go gopls gotools go-tools ];
in
{
default = pkgs.mkShell {
buildInputs = baseDeps;
};
});
# The default package for 'nix build'. This makes sense if the
# flake provides only one package or there is a clear "main"
# package.
defaultPackage = forAllSystems (system: self.packages.${system}.cloudflare-dns-ip);
overlay = final: prev: {
cloudflare-dns-ip = self.packages.${final.system}.cloudflare-dns-ip;
};
nixosModules.default = { config, lib, pkgs, ... }:
let
cfg = config.services.cloudflare-dns-ip;
in
{
options = with lib;{
services.cloudflare-dns-ip = {
enable = mkEnableOption "Enable cloudflare-dns-ip";
tokenPath = mkOption {
type = types.path;
default = "/var/lib/cloudflare-dns-ip/token";
description = "path to file containing the cloudflare token";
};
user = mkOption {
type = types.str;
default = "cloudflare-dns-ip";
description = "user cloudflare-dns-ip should run as";
};
group = mkOption {
type = types.str;
default = "cloudflare-dns-ip";
description = "group cloudflare-dns-ip should run as";
};
zone = mkOption {
type = types.str;
description = "the zone where the dns record is defined in cloudflare eg. 'burmudar.dev'";
};
record = mkOption {
type = types.str;
description = "the dns record to keep updated eg. www";
};
};
};
config = lib.mkIf cfg.enable {
users.users."${cfg.user}" = {
createHome = false;
group = "${cfg.group}";
isSystemUser = true;
isNormalUser = false;
description = "user for cloudflare-dns-ip service";
};
users.groups."${cfg.group}" = { };
system.activationScripts.cloudflare-lib-dir = lib.stringAfter ["var"] ''
mkdir -p /var/lib/cloudflare-dns-ip/
chown ${cfg.user}:${cfg.group} /var/lib/cloudflare-dns-ip/
'';
systemd.services.cloudflare-dns-ip = {
enable = true;
script =
''
${pkgs.cloudflare-dns-ip}/bin/cloudflare-dns-ip update -t ${cfg.tokenPath} -z ${cfg.zone} -r ${cfg.record}
'';
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
User = cfg.user;
Group = cfg.group;
};
};
};
};
formatter = forAllSystems (system: nixpkgsFor.${system}.nixpkgs-fmt);
};
}