···
{ config, lib, pkgs, ... }:
inherit (pkgs) ipfs runCommand makeWrapper;
cfg = config.services.ipfs;
10
-
ipfsFlags = ''${if cfg.autoMigrate then "--migrate" else ""} ${if cfg.enableGC then "--enable-gc" else ""} ${toString cfg.extraFlags}'';
8
+
ipfsFlags = toString ([
9
+
#(optionalString cfg.autoMount "--mount")
10
+
(optionalString cfg.autoMigrate "--migrate")
11
+
(optionalString cfg.enableGC "--enable-gc")
12
+
(optionalString (cfg.serviceFdlimit != null) "--manage-fdlimit=false")
13
+
(optionalString (cfg.defaultMode == "offline") "--offline")
14
+
(optionalString (cfg.defaultMode == "norouting") "--routing=none")
15
+
] ++ cfg.extraFlags);
12
-
# Before Version 17.09, ipfs would always use "/var/lib/ipfs/.ipfs" as it's dataDir
defaultDataDir = if versionAtLeast config.system.stateVersion "17.09" then
···
# Wrapping the ipfs binary with the environment variable IPFS_PATH set to dataDir because we can't set it in the user environment
wrapped = runCommand "ipfs" { buildInputs = [ makeWrapper ]; } ''
20
-
makeWrapper "${ipfs}/bin/ipfs" "$out/bin/ipfs" --set IPFS_PATH ${cfg.dataDir}
24
+
makeWrapper "${ipfs}/bin/ipfs" "$out/bin/ipfs" \
25
+
--set IPFS_PATH ${cfg.dataDir} \
26
+
--prefix PATH : /run/wrappers/bin
31
+
environment.IPFS_PATH = cfg.dataDir;
33
+
serviceConfig.User = cfg.user;
34
+
serviceConfig.Group = cfg.group;
37
+
baseService = recursiveUpdate commonEnv {
38
+
wants = [ "ipfs-init.service" ];
40
+
ipfs --local config Addresses.API ${cfg.apiAddress}
41
+
ipfs --local config Addresses.Gateway ${cfg.gatewayAddress}
42
+
'' + optionalString false/*cfg.autoMount*/ ''
43
+
ipfs --local config Mounts.FuseAllowOther --json true
44
+
ipfs --local config Mounts.IPFS ${cfg.ipfsMountDir}
45
+
ipfs --local config Mounts.IPNS ${cfg.ipnsMountDir}
46
+
'' + concatStringsSep "\n" (collect
50
+
# Using heredoc below so that the value is never improperly quoted
53
+
${builtins.toJSON value}
55
+
ipfs --local config --json "${concatStringsSep "." path}" "$value"
60
+
ExecStart = "${wrapped}/bin/ipfs daemon ${ipfsFlags}";
61
+
Restart = "on-failure";
63
+
} // optionalAttrs (cfg.serviceFdlimit != null) { LimitNOFILE = cfg.serviceFdlimit; };
···
107
+
#autoMount = mkOption {
108
+
# type = types.bool;
110
+
# description = "Whether IPFS should try to mount /ipfs and /ipns at startup.";
113
+
ipfsMountDir = mkOption {
116
+
description = "Where to mount the IPFS namespace to";
119
+
ipnsMountDir = mkOption {
122
+
description = "Where to mount the IPNS namespace to";
gatewayAddress = mkOption {
default = "/ip4/127.0.0.1/tcp/8080";
···
153
+
extraConfig = mkOption {
154
+
type = types.attrs;
155
+
description = toString [
156
+
"Attrset of daemon configuration to set using `ipfs config`, every time the daemon starts."
157
+
"These are applied last, so may override configuration set by other options in this module."
158
+
"Keep in mind that this configuration is stateful; i.e., unsetting anything in here does not reset the value to the default!"
162
+
Datastore.StorageMax = "100GB";
163
+
Discovery.MDNS.Enabled = false;
165
+
"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu"
166
+
"/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm"
168
+
Swarm.AddrFilters = null;
type = types.listOf types.str;
description = "Extra flags passed to the IPFS daemon";
179
+
serviceFdlimit = mkOption {
180
+
type = types.nullOr types.int;
183
+
The fdlimit for the IPFS systemd unit or `null` to have the daemon attempt to manage it.
185
+
example = 256*1024;
···
users.extraGroups = mkIf (cfg.group == "ipfs") {
119
-
gid = config.ids.gids.ipfs;
207
+
ipfs.gid = config.ids.gids.ipfs;
123
-
systemd.services.ipfs-init = {
210
+
systemd.services.ipfs-init = recursiveUpdate commonEnv {
description = "IPFS Initializer";
after = [ "local-fs.target" ];
before = [ "ipfs.service" "ipfs-offline.service" ];
129
-
environment.IPFS_PATH = cfg.dataDir;
131
-
path = [ pkgs.ipfs pkgs.su pkgs.bash ];
install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.dataDir}
218
+
'' + optionalString false/*cfg.autoMount*/ ''
219
+
install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.ipfsMountDir}
220
+
install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.ipnsMountDir}
if [[ ! -f ${cfg.dataDir}/config ]]; then
138
-
${ipfs}/bin/ipfs init ${optionalString cfg.emptyRepo "-e"}
224
+
ipfs init ${optionalString cfg.emptyRepo "-e"}
140
-
${ipfs}/bin/ipfs --local config Addresses.API ${cfg.apiAddress}
141
-
${ipfs}/bin/ipfs --local config Addresses.Gateway ${cfg.gatewayAddress}
PermissionsStartOnly = true;
153
-
systemd.services.ipfs = {
154
-
description = "IPFS Daemon";
235
+
# TODO These 3 definitions possibly be further abstracted through use of a function
236
+
# like: mutexServices "ipfs" [ "", "offline", "norouting" ] { ... shared conf here ... }
238
+
systemd.services.ipfs = recursiveUpdate baseService {
239
+
description = "IPFS Daemon";
wantedBy = mkIf (cfg.defaultMode == "online") [ "multi-user.target" ];
after = [ "network.target" "local-fs.target" "ipfs-init.service" ];
conflicts = [ "ipfs-offline.service" "ipfs-norouting.service"];
161
-
wants = [ "ipfs-init.service" ];
163
-
environment.IPFS_PATH = cfg.dataDir;
165
-
path = [ pkgs.ipfs ];
168
-
ExecStart = "${ipfs}/bin/ipfs daemon ${ipfsFlags}";
171
-
Restart = "on-failure";
176
-
systemd.services.ipfs-offline = {
245
+
systemd.services.ipfs-offline = recursiveUpdate baseService {
description = "IPFS Daemon (offline mode)";
wantedBy = mkIf (cfg.defaultMode == "offline") [ "multi-user.target" ];
after = [ "local-fs.target" "ipfs-init.service" ];
conflicts = [ "ipfs.service" "ipfs-norouting.service"];
184
-
wants = [ "ipfs-init.service" ];
186
-
environment.IPFS_PATH = cfg.dataDir;
188
-
path = [ pkgs.ipfs ];
191
-
ExecStart = "${ipfs}/bin/ipfs daemon ${ipfsFlags} --offline";
194
-
Restart = "on-failure";
199
-
systemd.services.ipfs-norouting = {
252
+
systemd.services.ipfs-norouting = recursiveUpdate baseService {
description = "IPFS Daemon (no routing mode)";
wantedBy = mkIf (cfg.defaultMode == "norouting") [ "multi-user.target" ];
after = [ "local-fs.target" "ipfs-init.service" ];
conflicts = [ "ipfs.service" "ipfs-offline.service"];
207
-
wants = [ "ipfs-init.service" ];
209
-
environment.IPFS_PATH = cfg.dataDir;
211
-
path = [ pkgs.ipfs ];
214
-
ExecStart = "${ipfs}/bin/ipfs daemon ${ipfsFlags} --routing=none";
217
-
Restart = "on-failure";