Merge pull request #90157 from obsidiansystems/socket-based-ipfs
Add socket-based IPFS support
This commit is contained in:
commit
1c4480e931
@ -1,69 +1,17 @@
|
|||||||
{ config, lib, pkgs, ... }:
|
{ config, lib, pkgs, options, ... }:
|
||||||
with lib;
|
with lib;
|
||||||
let
|
let
|
||||||
inherit (pkgs) ipfs runCommand makeWrapper;
|
|
||||||
|
|
||||||
cfg = config.services.ipfs;
|
cfg = config.services.ipfs;
|
||||||
|
opt = options.services.ipfs;
|
||||||
|
|
||||||
ipfsFlags = toString ([
|
ipfsFlags = toString ([
|
||||||
(optionalString cfg.autoMount "--mount")
|
(optionalString cfg.autoMount "--mount")
|
||||||
#(optionalString cfg.autoMigrate "--migrate")
|
|
||||||
(optionalString cfg.enableGC "--enable-gc")
|
(optionalString cfg.enableGC "--enable-gc")
|
||||||
(optionalString (cfg.serviceFdlimit != null) "--manage-fdlimit=false")
|
(optionalString (cfg.serviceFdlimit != null) "--manage-fdlimit=false")
|
||||||
(optionalString (cfg.defaultMode == "offline") "--offline")
|
(optionalString (cfg.defaultMode == "offline") "--offline")
|
||||||
(optionalString (cfg.defaultMode == "norouting") "--routing=none")
|
(optionalString (cfg.defaultMode == "norouting") "--routing=none")
|
||||||
] ++ cfg.extraFlags);
|
] ++ cfg.extraFlags);
|
||||||
|
|
||||||
defaultDataDir = if versionAtLeast config.system.stateVersion "17.09" then
|
|
||||||
"/var/lib/ipfs" else
|
|
||||||
"/var/lib/ipfs/.ipfs";
|
|
||||||
|
|
||||||
# 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 ]; preferLocalBuild = true; } ''
|
|
||||||
mkdir -p "$out/bin"
|
|
||||||
makeWrapper "${ipfs}/bin/ipfs" "$out/bin/ipfs" \
|
|
||||||
--set IPFS_PATH ${cfg.dataDir} \
|
|
||||||
--prefix PATH : /run/wrappers/bin
|
|
||||||
'';
|
|
||||||
|
|
||||||
|
|
||||||
commonEnv = {
|
|
||||||
environment.IPFS_PATH = cfg.dataDir;
|
|
||||||
path = [ wrapped ];
|
|
||||||
serviceConfig.User = cfg.user;
|
|
||||||
serviceConfig.Group = cfg.group;
|
|
||||||
};
|
|
||||||
|
|
||||||
baseService = recursiveUpdate commonEnv {
|
|
||||||
wants = [ "ipfs-init.service" ];
|
|
||||||
# NB: migration must be performed prior to pre-start, else we get the failure message!
|
|
||||||
preStart = optionalString cfg.autoMount ''
|
|
||||||
ipfs --local config Mounts.FuseAllowOther --json true
|
|
||||||
ipfs --local config Mounts.IPFS ${cfg.ipfsMountDir}
|
|
||||||
ipfs --local config Mounts.IPNS ${cfg.ipnsMountDir}
|
|
||||||
'' + concatStringsSep "\n" (collect
|
|
||||||
isString
|
|
||||||
(mapAttrsRecursive
|
|
||||||
(path: value:
|
|
||||||
# Using heredoc below so that the value is never improperly quoted
|
|
||||||
''
|
|
||||||
read value <<EOF
|
|
||||||
${builtins.toJSON value}
|
|
||||||
EOF
|
|
||||||
ipfs --local config --json "${concatStringsSep "." path}" "$value"
|
|
||||||
'')
|
|
||||||
({ Addresses.API = cfg.apiAddress;
|
|
||||||
Addresses.Gateway = cfg.gatewayAddress;
|
|
||||||
Addresses.Swarm = cfg.swarmAddress;
|
|
||||||
} //
|
|
||||||
cfg.extraConfig))
|
|
||||||
);
|
|
||||||
serviceConfig = {
|
|
||||||
ExecStart = "${wrapped}/bin/ipfs daemon ${ipfsFlags}";
|
|
||||||
Restart = "on-failure";
|
|
||||||
RestartSec = 1;
|
|
||||||
} // optionalAttrs (cfg.serviceFdlimit != null) { LimitNOFILE = cfg.serviceFdlimit; };
|
|
||||||
};
|
|
||||||
in {
|
in {
|
||||||
|
|
||||||
###### interface
|
###### interface
|
||||||
@ -88,7 +36,9 @@ in {
|
|||||||
|
|
||||||
dataDir = mkOption {
|
dataDir = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = defaultDataDir;
|
default = if versionAtLeast config.system.stateVersion "17.09"
|
||||||
|
then "/var/lib/ipfs"
|
||||||
|
else "/var/lib/ipfs/.ipfs";
|
||||||
description = "The data dir for IPFS";
|
description = "The data dir for IPFS";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -98,18 +48,6 @@ in {
|
|||||||
description = "systemd service that is enabled by default";
|
description = "systemd service that is enabled by default";
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
autoMigrate = mkOption {
|
|
||||||
type = types.bool;
|
|
||||||
default = false;
|
|
||||||
description = ''
|
|
||||||
Whether IPFS should try to migrate the file system automatically.
|
|
||||||
|
|
||||||
The daemon will need to be able to download a binary from https://ipfs.io to perform the migration.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
autoMount = mkOption {
|
autoMount = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = false;
|
default = false;
|
||||||
@ -199,13 +137,21 @@ in {
|
|||||||
example = 64*1024;
|
example = 64*1024;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
startWhenNeeded = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to use socket activation to start IPFS when needed.";
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
###### implementation
|
###### implementation
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
environment.systemPackages = [ wrapped ];
|
environment.systemPackages = [ pkgs.ipfs ];
|
||||||
|
environment.variables.IPFS_PATH = cfg.dataDir;
|
||||||
|
|
||||||
programs.fuse = mkIf cfg.autoMount {
|
programs.fuse = mkIf cfg.autoMount {
|
||||||
userAllowOther = true;
|
userAllowOther = true;
|
||||||
};
|
};
|
||||||
@ -234,10 +180,14 @@ in {
|
|||||||
"d '${cfg.ipnsMountDir}' - ${cfg.user} ${cfg.group} - -"
|
"d '${cfg.ipnsMountDir}' - ${cfg.user} ${cfg.group} - -"
|
||||||
];
|
];
|
||||||
|
|
||||||
systemd.services.ipfs-init = recursiveUpdate commonEnv {
|
systemd.packages = [ pkgs.ipfs ];
|
||||||
|
|
||||||
|
systemd.services.ipfs-init = {
|
||||||
description = "IPFS Initializer";
|
description = "IPFS Initializer";
|
||||||
|
|
||||||
before = [ "ipfs.service" "ipfs-offline.service" "ipfs-norouting.service" ];
|
environment.IPFS_PATH = cfg.dataDir;
|
||||||
|
|
||||||
|
path = [ pkgs.ipfs ];
|
||||||
|
|
||||||
script = ''
|
script = ''
|
||||||
if [[ ! -f ${cfg.dataDir}/config ]]; then
|
if [[ ! -f ${cfg.dataDir}/config ]]; then
|
||||||
@ -251,34 +201,63 @@ in {
|
|||||||
fi
|
fi
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
wantedBy = [ "default.target" ];
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
RemainAfterExit = true;
|
RemainAfterExit = true;
|
||||||
|
User = cfg.user;
|
||||||
|
Group = cfg.group;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
# TODO These 3 definitions possibly be further abstracted through use of a function
|
systemd.services.ipfs = {
|
||||||
# like: mutexServices "ipfs" [ "", "offline", "norouting" ] { ... shared conf here ... }
|
path = [ "/run/wrappers" pkgs.ipfs ];
|
||||||
|
environment.IPFS_PATH = cfg.dataDir;
|
||||||
|
|
||||||
systemd.services.ipfs = recursiveUpdate baseService {
|
wants = [ "ipfs-init.service" ];
|
||||||
description = "IPFS Daemon";
|
after = [ "ipfs-init.service" ];
|
||||||
wantedBy = mkIf (cfg.defaultMode == "online") [ "multi-user.target" ];
|
|
||||||
after = [ "network.target" "ipfs-init.service" ];
|
preStart = optionalString cfg.autoMount ''
|
||||||
conflicts = [ "ipfs-offline.service" "ipfs-norouting.service"];
|
ipfs --local config Mounts.FuseAllowOther --json true
|
||||||
|
ipfs --local config Mounts.IPFS ${cfg.ipfsMountDir}
|
||||||
|
ipfs --local config Mounts.IPNS ${cfg.ipnsMountDir}
|
||||||
|
'' + concatStringsSep "\n" (collect
|
||||||
|
isString
|
||||||
|
(mapAttrsRecursive
|
||||||
|
(path: value:
|
||||||
|
# Using heredoc below so that the value is never improperly quoted
|
||||||
|
''
|
||||||
|
read value <<EOF
|
||||||
|
${builtins.toJSON value}
|
||||||
|
EOF
|
||||||
|
ipfs --local config --json "${concatStringsSep "." path}" "$value"
|
||||||
|
'')
|
||||||
|
({ Addresses.API = cfg.apiAddress;
|
||||||
|
Addresses.Gateway = cfg.gatewayAddress;
|
||||||
|
Addresses.Swarm = cfg.swarmAddress;
|
||||||
|
} //
|
||||||
|
cfg.extraConfig))
|
||||||
|
);
|
||||||
|
serviceConfig = {
|
||||||
|
ExecStart = ["" "${pkgs.ipfs}/bin/ipfs daemon ${ipfsFlags}"];
|
||||||
|
User = cfg.user;
|
||||||
|
Group = cfg.group;
|
||||||
|
} // optionalAttrs (cfg.serviceFdlimit != null) { LimitNOFILE = cfg.serviceFdlimit; };
|
||||||
|
} // optionalAttrs (!cfg.startWhenNeeded) {
|
||||||
|
wantedBy = [ "default.target" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services.ipfs-offline = recursiveUpdate baseService {
|
systemd.sockets.ipfs-gateway = {
|
||||||
description = "IPFS Daemon (offline mode)";
|
wantedBy = [ "sockets.target" ];
|
||||||
wantedBy = mkIf (cfg.defaultMode == "offline") [ "multi-user.target" ];
|
socketConfig.ListenStream = [ "" ]
|
||||||
after = [ "ipfs-init.service" ];
|
++ lib.optional (cfg.gatewayAddress == opt.gatewayAddress.default) [ "127.0.0.1:8080" "[::1]:8080" ];
|
||||||
conflicts = [ "ipfs.service" "ipfs-norouting.service"];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services.ipfs-norouting = recursiveUpdate baseService {
|
systemd.sockets.ipfs-api = {
|
||||||
description = "IPFS Daemon (no routing mode)";
|
wantedBy = [ "sockets.target" ];
|
||||||
wantedBy = mkIf (cfg.defaultMode == "norouting") [ "multi-user.target" ];
|
socketConfig.ListenStream = [ "" "%t/ipfs.sock" ]
|
||||||
after = [ "ipfs-init.service" ];
|
++ lib.optional (cfg.apiAddress == opt.apiAddress.default) [ "127.0.0.1:5001" "[::1]:5001" ];
|
||||||
conflicts = [ "ipfs.service" "ipfs-offline.service"];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -21,5 +21,12 @@ import ./make-test-python.nix ({ pkgs, ...} : {
|
|||||||
)
|
)
|
||||||
|
|
||||||
machine.succeed(f"ipfs cat /ipfs/{ipfs_hash.strip()} | grep fnord")
|
machine.succeed(f"ipfs cat /ipfs/{ipfs_hash.strip()} | grep fnord")
|
||||||
|
|
||||||
|
ipfs_hash = machine.succeed(
|
||||||
|
"echo fnord2 | ipfs --api /unix/run/ipfs.sock add | awk '{ print $2 }'"
|
||||||
|
)
|
||||||
|
machine.succeed(
|
||||||
|
f"ipfs --api /unix/run/ipfs.sock cat /ipfs/{ipfs_hash.strip()} | grep fnord2"
|
||||||
|
)
|
||||||
'';
|
'';
|
||||||
})
|
})
|
||||||
|
@ -26,6 +26,14 @@ buildGoModule rec {
|
|||||||
|
|
||||||
vendorSha256 = null;
|
vendorSha256 = null;
|
||||||
|
|
||||||
|
postInstall = ''
|
||||||
|
install -D misc/systemd/ipfs.service $out/etc/systemd/system/ipfs.service
|
||||||
|
install -D misc/systemd/ipfs-api.socket $out/etc/systemd/system/ipfs-api.socket
|
||||||
|
install -D misc/systemd/ipfs-gateway.socket $out/etc/systemd/system/ipfs-gateway.socket
|
||||||
|
substituteInPlace $out/etc/systemd/system/ipfs.service \
|
||||||
|
--replace /usr/bin/ipfs $out/bin/ipfs
|
||||||
|
'';
|
||||||
|
|
||||||
meta = with stdenv.lib; {
|
meta = with stdenv.lib; {
|
||||||
description = "A global, versioned, peer-to-peer filesystem";
|
description = "A global, versioned, peer-to-peer filesystem";
|
||||||
homepage = "https://ipfs.io/";
|
homepage = "https://ipfs.io/";
|
||||||
|
Loading…
Reference in New Issue
Block a user