git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #90157 from obsidiansystems/socket-based-ipfs

Add socket-based IPFS support

commit1c4480e9318fa211568dcb6b71ff039ae9c5595a authorJohn Ericson <git@JohnEricson.me> dateFri 12 Jun 2020, 18:31 (6 years ago) parent2fc097a2c2f6c0
3 files changed+81−87
nixos/modules/services/network-filesystems/ipfs.nix+66−87 nixos/tests/ipfs.nix+7 pkgs/applications/networking/ipfs/default.nix+8
nixos/modules/services/network-filesystems/ipfs.nix+66−87
@@ -1,69 +1,17 @@
1−{ config, lib, pkgs, ... }:
1+{ config, lib, pkgs, options, ... }:
22with lib;
33let
4− inherit (pkgs) ipfs runCommand makeWrapper;
5−
64 cfg = config.services.ipfs;
5+ opt = options.services.ipfs;
76
87 ipfsFlags = toString ([
98 (optionalString cfg.autoMount "--mount")
10− #(optionalString cfg.autoMigrate "--migrate")
119 (optionalString cfg.enableGC "--enable-gc")
1210 (optionalString (cfg.serviceFdlimit != null) "--manage-fdlimit=false")
1311 (optionalString (cfg.defaultMode == "offline") "--offline")
1412 (optionalString (cfg.defaultMode == "norouting") "--routing=none")
1513 ] ++ cfg.extraFlags);
1614
17− defaultDataDir = if versionAtLeast config.system.stateVersion "17.09" then
18− "/var/lib/ipfs" else
19− "/var/lib/ipfs/.ipfs";
20−
21− # Wrapping the ipfs binary with the environment variable IPFS_PATH set to dataDir because we can't set it in the user environment
22− wrapped = runCommand "ipfs" { buildInputs = [ makeWrapper ]; preferLocalBuild = true; } ''
23− mkdir -p "$out/bin"
24− makeWrapper "${ipfs}/bin/ipfs" "$out/bin/ipfs" \
25− --set IPFS_PATH ${cfg.dataDir} \
26− --prefix PATH : /run/wrappers/bin
27− '';
28−
29−
30− commonEnv = {
31− environment.IPFS_PATH = cfg.dataDir;
32− path = [ wrapped ];
33− serviceConfig.User = cfg.user;
34− serviceConfig.Group = cfg.group;
35− };
36−
37− baseService = recursiveUpdate commonEnv {
38− wants = [ "ipfs-init.service" ];
39− # NB: migration must be performed prior to pre-start, else we get the failure message!
40− preStart = optionalString cfg.autoMount ''
41− ipfs --local config Mounts.FuseAllowOther --json true
42− ipfs --local config Mounts.IPFS ${cfg.ipfsMountDir}
43− ipfs --local config Mounts.IPNS ${cfg.ipnsMountDir}
44− '' + concatStringsSep "\n" (collect
45− isString
46− (mapAttrsRecursive
47− (path: value:
48− # Using heredoc below so that the value is never improperly quoted
49− ''
50− read value <<EOF
51− ${builtins.toJSON value}
52− EOF
53− ipfs --local config --json "${concatStringsSep "." path}" "$value"
54− '')
55− ({ Addresses.API = cfg.apiAddress;
56− Addresses.Gateway = cfg.gatewayAddress;
57− Addresses.Swarm = cfg.swarmAddress;
58− } //
59− cfg.extraConfig))
60− );
61− serviceConfig = {
62− ExecStart = "${wrapped}/bin/ipfs daemon ${ipfsFlags}";
63− Restart = "on-failure";
64− RestartSec = 1;
65− } // optionalAttrs (cfg.serviceFdlimit != null) { LimitNOFILE = cfg.serviceFdlimit; };
66− };
6715in {
6816
6917 ###### interface
@@ -88,7 +36,9 @@ in {
8836
8937 dataDir = mkOption {
9038 type = types.str;
91− default = defaultDataDir;
39+ default = if versionAtLeast config.system.stateVersion "17.09"
40+ then "/var/lib/ipfs"
41+ else "/var/lib/ipfs/.ipfs";
9242 description = "The data dir for IPFS";
9343 };
9444
@@ -98,18 +48,6 @@ in {
9848 description = "systemd service that is enabled by default";
9949 };
10050
101− /*
102− autoMigrate = mkOption {
103− type = types.bool;
104− default = false;
105− description = ''
106− Whether IPFS should try to migrate the file system automatically.
107−
108− The daemon will need to be able to download a binary from https://ipfs.io to perform the migration.
109− '';
110− };
111− */
112−
11351 autoMount = mkOption {
11452 type = types.bool;
11553 default = false;
@@ -199,13 +137,21 @@ in {
199137 example = 64*1024;
200138 };
201139
140+ startWhenNeeded = mkOption {
141+ type = types.bool;
142+ default = false;
143+ description = "Whether to use socket activation to start IPFS when needed.";
144+ };
145+
202146 };
203147 };
204148
205149 ###### implementation
206150
207151 config = mkIf cfg.enable {
208− environment.systemPackages = [ wrapped ];
152+ environment.systemPackages = [ pkgs.ipfs ];
153+ environment.variables.IPFS_PATH = cfg.dataDir;
154+
209155 programs.fuse = mkIf cfg.autoMount {
210156 userAllowOther = true;
211157 };
@@ -234,10 +180,14 @@ in {
234180 "d '${cfg.ipnsMountDir}' - ${cfg.user} ${cfg.group} - -"
235181 ];
236182
237− systemd.services.ipfs-init = recursiveUpdate commonEnv {
183+ systemd.packages = [ pkgs.ipfs ];
184+
185+ systemd.services.ipfs-init = {
238186 description = "IPFS Initializer";
239187
240− before = [ "ipfs.service" "ipfs-offline.service" "ipfs-norouting.service" ];
188+ environment.IPFS_PATH = cfg.dataDir;
189+
190+ path = [ pkgs.ipfs ];
241191
242192 script = ''
243193 if [[ ! -f ${cfg.dataDir}/config ]]; then
@@ -251,34 +201,63 @@ in {
251201 fi
252202 '';
253203
204+ wantedBy = [ "default.target" ];
205+
254206 serviceConfig = {
255207 Type = "oneshot";
256208 RemainAfterExit = true;
209+ User = cfg.user;
210+ Group = cfg.group;
257211 };
258212 };
259213
260− # TODO These 3 definitions possibly be further abstracted through use of a function
261− # like: mutexServices "ipfs" [ "", "offline", "norouting" ] { ... shared conf here ... }
214+ systemd.services.ipfs = {
215+ path = [ "/run/wrappers" pkgs.ipfs ];
216+ environment.IPFS_PATH = cfg.dataDir;
217+
218+ wants = [ "ipfs-init.service" ];
219+ after = [ "ipfs-init.service" ];
262220
263− systemd.services.ipfs = recursiveUpdate baseService {
264− description = "IPFS Daemon";
265− wantedBy = mkIf (cfg.defaultMode == "online") [ "multi-user.target" ];
266− after = [ "network.target" "ipfs-init.service" ];
267− conflicts = [ "ipfs-offline.service" "ipfs-norouting.service"];
221+ preStart = optionalString cfg.autoMount ''
222+ ipfs --local config Mounts.FuseAllowOther --json true
223+ ipfs --local config Mounts.IPFS ${cfg.ipfsMountDir}
224+ ipfs --local config Mounts.IPNS ${cfg.ipnsMountDir}
225+ '' + concatStringsSep "\n" (collect
226+ isString
227+ (mapAttrsRecursive
228+ (path: value:
229+ # Using heredoc below so that the value is never improperly quoted
230+ ''
231+ read value <<EOF
232+ ${builtins.toJSON value}
233+ EOF
234+ ipfs --local config --json "${concatStringsSep "." path}" "$value"
235+ '')
236+ ({ Addresses.API = cfg.apiAddress;
237+ Addresses.Gateway = cfg.gatewayAddress;
238+ Addresses.Swarm = cfg.swarmAddress;
239+ } //
240+ cfg.extraConfig))
241+ );
242+ serviceConfig = {
243+ ExecStart = ["" "${pkgs.ipfs}/bin/ipfs daemon ${ipfsFlags}"];
244+ User = cfg.user;
245+ Group = cfg.group;
246+ } // optionalAttrs (cfg.serviceFdlimit != null) { LimitNOFILE = cfg.serviceFdlimit; };
247+ } // optionalAttrs (!cfg.startWhenNeeded) {
248+ wantedBy = [ "default.target" ];
268249 };
269250
270− systemd.services.ipfs-offline = recursiveUpdate baseService {
271− description = "IPFS Daemon (offline mode)";
272− wantedBy = mkIf (cfg.defaultMode == "offline") [ "multi-user.target" ];
273− after = [ "ipfs-init.service" ];
274− conflicts = [ "ipfs.service" "ipfs-norouting.service"];
251+ systemd.sockets.ipfs-gateway = {
252+ wantedBy = [ "sockets.target" ];
253+ socketConfig.ListenStream = [ "" ]
254+ ++ lib.optional (cfg.gatewayAddress == opt.gatewayAddress.default) [ "127.0.0.1:8080" "[::1]:8080" ];
275255 };
276256
277− systemd.services.ipfs-norouting = recursiveUpdate baseService {
278− description = "IPFS Daemon (no routing mode)";
279− wantedBy = mkIf (cfg.defaultMode == "norouting") [ "multi-user.target" ];
280− after = [ "ipfs-init.service" ];
281− conflicts = [ "ipfs.service" "ipfs-offline.service"];
257+ systemd.sockets.ipfs-api = {
258+ wantedBy = [ "sockets.target" ];
259+ socketConfig.ListenStream = [ "" "%t/ipfs.sock" ]
260+ ++ lib.optional (cfg.apiAddress == opt.apiAddress.default) [ "127.0.0.1:5001" "[::1]:5001" ];
282261 };
283262
284263 };
nixos/tests/ipfs.nix+7
@@ -21,5 +21,12 @@ import ./make-test-python.nix ({ pkgs, ...} : {
2121 )
2222
2323 machine.succeed(f"ipfs cat /ipfs/{ipfs_hash.strip()} | grep fnord")
24+
25+ ipfs_hash = machine.succeed(
26+ "echo fnord2 | ipfs --api /unix/run/ipfs.sock add | awk '{ print $2 }'"
27+ )
28+ machine.succeed(
29+ f"ipfs --api /unix/run/ipfs.sock cat /ipfs/{ipfs_hash.strip()} | grep fnord2"
30+ )
2431 '';
2532})
pkgs/applications/networking/ipfs/default.nix+8
@@ -26,6 +26,14 @@ buildGoModule rec {
2626
2727 vendorSha256 = null;
2828
29+ postInstall = ''
30+ install -D misc/systemd/ipfs.service $out/etc/systemd/system/ipfs.service
31+ install -D misc/systemd/ipfs-api.socket $out/etc/systemd/system/ipfs-api.socket
32+ install -D misc/systemd/ipfs-gateway.socket $out/etc/systemd/system/ipfs-gateway.socket
33+ substituteInPlace $out/etc/systemd/system/ipfs.service \
34+ --replace /usr/bin/ipfs $out/bin/ipfs
35+ '';
36+
2937 meta = with stdenv.lib; {
3038 description = "A global, versioned, peer-to-peer filesystem";
3139 homepage = "https://ipfs.io/";