2022-04-15 18:54:38 +00:00
{ config , lib , pkgs , utils , . . . }:
2016-10-30 19:39:53 +00:00
with lib ;
let
cfg = config . services . ipfs ;
2022-03-06 15:57:57 +00:00
ipfsFlags = utils . escapeSystemdExecArgs (
optional cfg . autoMount " - - m o u n t " ++
optional cfg . enableGC " - - e n a b l e - g c " ++
optional ( cfg . serviceFdlimit != null ) " - - m a n a g e - f d l i m i t = f a l s e " ++
optional ( cfg . defaultMode == " o f f l i n e " ) " - - o f f l i n e " ++
optional ( cfg . defaultMode == " n o r o u t i n g " ) " - - r o u t i n g = n o n e " ++
cfg . extraFlags
) ;
2016-10-30 19:39:53 +00:00
2021-09-08 14:50:36 +00:00
profile =
if cfg . localDiscovery
then " l o c a l - d i s c o v e r y "
else " s e r v e r " ;
2020-06-17 20:19:01 +00:00
splitMulitaddr = addrRaw : lib . tail ( lib . splitString " / " addrRaw ) ;
2021-08-31 00:16:55 +00:00
multiaddrToListenStream = addrRaw :
let
2020-06-17 20:19:01 +00:00
addr = splitMulitaddr addrRaw ;
s = builtins . elemAt addr ;
2021-08-31 00:16:55 +00:00
in
if s 0 == " i p 4 " && s 2 == " t c p "
then " ${ s 1 } : ${ s 3 } "
2020-06-17 20:19:01 +00:00
else if s 0 == " i p 6 " && s 2 == " t c p "
2021-08-31 00:16:55 +00:00
then " [ ${ s 1 } ] : ${ s 3 } "
2020-06-17 20:19:01 +00:00
else if s 0 == " u n i x "
2021-08-31 00:16:55 +00:00
then " / ${ lib . concatStringsSep " / " ( lib . tail addr ) } "
2020-06-17 20:19:01 +00:00
else null ; # not valid for listen stream, skip
2021-08-31 00:16:55 +00:00
multiaddrToListenDatagram = addrRaw :
let
2020-08-11 21:46:39 +00:00
addr = splitMulitaddr addrRaw ;
s = builtins . elemAt addr ;
2021-08-31 00:16:55 +00:00
in
if s 0 == " i p 4 " && s 2 == " u d p "
then " ${ s 1 } : ${ s 3 } "
2020-08-11 21:46:39 +00:00
else if s 0 == " i p 6 " && s 2 == " u d p "
2021-08-31 00:16:55 +00:00
then " [ ${ s 1 } ] : ${ s 3 } "
2020-08-11 21:46:39 +00:00
else null ; # not valid for listen datagram, skip
2021-08-31 00:16:55 +00:00
in
{
2016-10-30 19:39:53 +00:00
###### interface
options = {
services . ipfs = {
2018-12-22 19:04:19 +00:00
enable = mkEnableOption " I n t e r p l a n e t a r y F i l e S y s t e m ( W A R N I N G : m a y c a u s e s e v e r e n e t w o r k d e g r e d a t i o n ) " ;
2016-10-30 19:39:53 +00:00
2020-11-03 23:35:06 +00:00
package = mkOption {
type = types . package ;
default = pkgs . ipfs ;
2021-10-03 16:06:03 +00:00
defaultText = literalExpression " p k g s . i p f s " ;
2020-11-03 23:35:06 +00:00
description = " W h i c h I P F S p a c k a g e t o u s e . " ;
} ;
2016-10-30 19:39:53 +00:00
user = mkOption {
type = types . str ;
default = " i p f s " ;
description = " U s e r u n d e r w h i c h t h e I P F S d a e m o n r u n s " ;
} ;
group = mkOption {
type = types . str ;
default = " i p f s " ;
description = " G r o u p u n d e r w h i c h t h e I P F S d a e m o n r u n s " ;
} ;
dataDir = mkOption {
type = types . str ;
2021-08-31 00:16:55 +00:00
default =
if versionAtLeast config . system . stateVersion " 1 7 . 0 9 "
then " / v a r / l i b / i p f s "
else " / v a r / l i b / i p f s / . i p f s " ;
2021-11-26 00:16:05 +00:00
defaultText = literalExpression ''
if versionAtLeast config . system . stateVersion " 1 7 . 0 9 "
then " / v a r / l i b / i p f s "
else " / v a r / l i b / i p f s / . i p f s "
'' ;
2016-10-30 19:39:53 +00:00
description = " T h e d a t a d i r f o r I P F S " ;
} ;
2017-08-12 13:40:05 +00:00
defaultMode = mkOption {
type = types . enum [ " o n l i n e " " o f f l i n e " " n o r o u t i n g " ] ;
default = " o n l i n e " ;
2017-09-06 22:40:42 +00:00
description = " s y s t e m d s e r v i c e t h a t i s e n a b l e d b y d e f a u l t " ;
2017-08-12 13:40:05 +00:00
} ;
2017-09-16 16:03:37 +00:00
autoMount = mkOption {
type = types . bool ;
default = false ;
description = " W h e t h e r I P F S s h o u l d t r y t o m o u n t / i p f s a n d / i p n s a t s t a r t u p . " ;
} ;
2017-08-30 15:24:09 +00:00
2021-08-31 13:22:36 +00:00
autoMigrate = mkOption {
type = types . bool ;
default = true ;
description = " W h e t h e r I P F S s h o u l d t r y t o r u n t h e f s - r e p o - m i g r a t i o n a t s t a r t u p . " ;
} ;
2017-08-30 15:24:09 +00:00
ipfsMountDir = mkOption {
type = types . str ;
default = " / i p f s " ;
description = " W h e r e t o m o u n t t h e I P F S n a m e s p a c e t o " ;
} ;
ipnsMountDir = mkOption {
type = types . str ;
default = " / i p n s " ;
description = " W h e r e t o m o u n t t h e I P N S n a m e s p a c e t o " ;
2017-08-08 08:52:08 +00:00
} ;
2016-11-28 14:24:09 +00:00
gatewayAddress = mkOption {
type = types . str ;
default = " / i p 4 / 1 2 7 . 0 . 0 . 1 / t c p / 8 0 8 0 " ;
description = " W h e r e t h e I P F S G a t e w a y c a n b e r e a c h e d " ;
} ;
apiAddress = mkOption {
type = types . str ;
default = " / i p 4 / 1 2 7 . 0 . 0 . 1 / t c p / 5 0 0 1 " ;
description = " W h e r e I P F S e x p o s e s i t s A P I t o " ;
} ;
2017-11-26 06:47:59 +00:00
swarmAddress = mkOption {
type = types . listOf types . str ;
2020-06-17 20:19:01 +00:00
default = [
" / i p 4 / 0 . 0 . 0 . 0 / t c p / 4 0 0 1 "
" / i p 6 / : : / t c p / 4 0 0 1 "
2020-08-08 19:45:57 +00:00
" / i p 4 / 0 . 0 . 0 . 0 / u d p / 4 0 0 1 / q u i c "
" / i p 6 / : : / u d p / 4 0 0 1 / q u i c "
2020-06-17 20:19:01 +00:00
] ;
2017-11-26 06:47:59 +00:00
description = " W h e r e I P F S l i s t e n s f o r i n c o m i n g p 2 p c o n n e c t i o n s " ;
} ;
2016-10-30 19:39:53 +00:00
enableGC = mkOption {
type = types . bool ;
default = false ;
2017-09-06 22:40:42 +00:00
description = " W h e t h e r t o e n a b l e a u t o m a t i c g a r b a g e c o l l e c t i o n " ;
2016-10-30 19:39:53 +00:00
} ;
2017-01-13 18:28:06 +00:00
emptyRepo = mkOption {
type = types . bool ;
default = false ;
2017-09-06 22:40:42 +00:00
description = " I f s e t t o t r u e , t h e r e p o w o n ' t b e i n i t i a l i z e d w i t h h e l p f i l e s " ;
2017-01-13 18:28:06 +00:00
} ;
2017-08-27 16:52:56 +00:00
extraConfig = mkOption {
type = types . attrs ;
2017-09-06 22:40:42 +00:00
description = ''
Attrset of daemon configuration to set using <command> ipfs config < /command > , every time the daemon starts .
These are applied last , so may override configuration set by other options in this module .
Keep in mind that this configuration is stateful ; i . e . , unsetting anything in here does not reset the value to the default !
'' ;
2021-08-31 00:16:55 +00:00
default = { } ;
2017-08-27 16:52:56 +00:00
example = {
Datastore . StorageMax = " 1 0 0 G B " ;
Discovery . MDNS . Enabled = false ;
Bootstrap = [
" / i p 4 / 1 2 8 . 1 9 9 . 2 1 9 . 1 1 1 / t c p / 4 0 0 1 / i p f s / Q m S o L S a f T M B s P K a d T E g a X c t D Q V c q N 8 8 C N L H X M k T N w M K P n u "
" / i p 4 / 1 6 2 . 2 4 3 . 2 4 8 . 2 1 3 / t c p / 4 0 0 1 / i p f s / Q m S o L u e R 4 x B e U b Y 9 W Z 9 x G U U x u n b K W c r N F T D A a d Q J m o c n W m "
] ;
Swarm . AddrFilters = null ;
} ;
} ;
2016-10-30 19:39:53 +00:00
extraFlags = mkOption {
type = types . listOf types . str ;
description = " E x t r a f l a g s p a s s e d t o t h e I P F S d a e m o n " ;
2021-08-31 00:16:55 +00:00
default = [ ] ;
2016-10-30 19:39:53 +00:00
} ;
2017-08-27 16:52:56 +00:00
2018-05-21 12:15:58 +00:00
localDiscovery = mkOption {
type = types . bool ;
description = '' W h e t h e r t o e n a b l e l o c a l d i s c o v e r y f o r t h e i p f s d a e m o n .
2018-05-23 14:44:31 +00:00
This will allow ipfs to scan ports on your local network . Some hosting services will ban you if you do this .
2018-05-21 12:15:58 +00:00
'' ;
2021-09-08 15:00:29 +00:00
default = false ;
2018-05-21 12:15:58 +00:00
} ;
2017-07-31 14:15:02 +00:00
serviceFdlimit = mkOption {
type = types . nullOr types . int ;
default = null ;
2017-09-06 22:40:42 +00:00
description = " T h e f d l i m i t f o r t h e I P F S s y s t e m d u n i t o r < l i t e r a l > n u l l < / l i t e r a l > t o h a v e t h e d a e m o n a t t e m p t t o m a n a g e i t " ;
2021-08-31 00:16:55 +00:00
example = 64 * 1024 ;
2017-07-31 14:15:02 +00:00
} ;
2020-06-11 20:45:39 +00:00
startWhenNeeded = mkOption {
type = types . bool ;
default = false ;
description = " W h e t h e r t o u s e s o c k e t a c t i v a t i o n t o s t a r t I P F S w h e n n e e d e d . " ;
} ;
2016-10-30 19:39:53 +00:00
} ;
} ;
###### implementation
config = mkIf cfg . enable {
2020-11-03 23:35:06 +00:00
environment . systemPackages = [ cfg . package ] ;
2020-06-11 20:27:22 +00:00
environment . variables . IPFS_PATH = cfg . dataDir ;
2021-08-31 00:17:57 +00:00
# https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size
boot . kernel . sysctl . " n e t . c o r e . r m e m _ m a x " = mkDefault 2500000 ;
2019-08-11 13:36:33 +00:00
programs . fuse = mkIf cfg . autoMount {
userAllowOther = true ;
} ;
2016-10-30 19:39:53 +00:00
2018-06-29 23:58:35 +00:00
users . users = mkIf ( cfg . user == " i p f s " ) {
2016-10-30 19:39:53 +00:00
ipfs = {
group = cfg . group ;
home = cfg . dataDir ;
createHome = false ;
uid = config . ids . uids . ipfs ;
description = " I P F S d a e m o n u s e r " ;
2020-05-13 06:59:49 +00:00
packages = [
pkgs . ipfs-migrator
] ;
2016-10-30 19:39:53 +00:00
} ;
} ;
2018-06-29 23:58:35 +00:00
users . groups = mkIf ( cfg . group == " i p f s " ) {
2017-08-28 20:09:39 +00:00
ipfs . gid = config . ids . gids . ipfs ;
2016-10-30 19:39:53 +00:00
} ;
2019-02-24 17:49:02 +00:00
systemd . tmpfiles . rules = [
" d ' ${ cfg . dataDir } ' - ${ cfg . user } ${ cfg . group } - - "
] ++ optionals cfg . autoMount [
" d ' ${ cfg . ipfsMountDir } ' - ${ cfg . user } ${ cfg . group } - - "
" d ' ${ cfg . ipnsMountDir } ' - ${ cfg . user } ${ cfg . group } - - "
] ;
2022-03-17 15:40:54 +00:00
# The hardened systemd unit breaks the fuse-mount function according to documentation in the unit file itself
systemd . packages = if cfg . autoMount
then [ cfg . package . systemd_unit ]
else [ cfg . package . systemd_unit_hardened ] ;
2020-06-11 21:59:11 +00:00
2021-04-22 18:04:26 +00:00
systemd . services . ipfs = {
path = [ " / r u n / w r a p p e r s " cfg . package ] ;
2020-06-11 20:27:22 +00:00
environment . IPFS_PATH = cfg . dataDir ;
2021-04-22 18:04:26 +00:00
preStart = ''
2021-09-08 14:50:36 +00:00
if [ [ ! - f " $ I P F S _ P A T H / c o n f i g " ] ] ; then
ipfs init $ { optionalString cfg . emptyRepo " - e " } - - profile = $ { profile }
2018-05-23 14:44:31 +00:00
else
2021-09-08 14:50:36 +00:00
# After an unclean shutdown this file may exist which will cause the config command to attempt to talk to the daemon. This will hang forever if systemd is holding our sockets open.
rm - vf " $ I P F S _ P A T H / a p i "
ipfs - - offline config profile apply $ { profile }
2017-01-13 18:28:06 +00:00
fi
2021-04-22 18:04:26 +00:00
'' + o p t i o n a l S t r i n g c f g . a u t o M o u n t ''
2021-05-17 08:36:56 +00:00
ipfs - - offline config Mounts . FuseAllowOther - - json true
ipfs - - offline config Mounts . IPFS $ { cfg . ipfsMountDir }
ipfs - - offline config Mounts . IPNS $ { cfg . ipnsMountDir }
2021-08-31 13:22:36 +00:00
'' + o p t i o n a l S t r i n g c f g . a u t o M i g r a t e ''
ipfs-migrator: 1.7.1 -> 2.0.2
https://github.com/ipfs/fs-repo-migrations/releases/tag/v2.0.2
This is pretty much a complete rewrite of the ipfs-migrator package.
In version 2.0.0 a major change was made to the way the migrator works. Before, there was one binary that contained every migration. Now every migration has its own binary. If fs-repo-migrations can't find a required binary in the PATH, it will download it off the internet. To prevent that, build every migration individually, symlink them all into one package and then wrap fs-repo-migrations so it finds the package with all the migrations.
The change to the IPFS NixOS module and the IPFS package is needed because without explicitly specifying a repo version to migrate to, fs-repo-migrations will query the internet to find the latest version. This fails in the sandbox, for example when testing the ipfs passthru tests.
While it may seem like the repoVersion and IPFS version are in sync and the code could be simplified, this is not the case. See https://github.com/ipfs/fs-repo-migrations#when-should-i-migrate for a table with the IPFS versions and corresponding repo versions.
Go 1.17 breaks the migrations, so use Go 1.16 instead. This is also the Go version used in their CI, see https://github.com/ipfs/fs-repo-migrations/blob/3dc218e3006adac25e1cb5e969d7c9d961f15ddd/.github/workflows/test.yml#L4. See https://github.com/ipfs/fs-repo-migrations/pull/140#issuecomment-982715907 for a previous mention of this issue. The issue manifests itself when doing anything with a migration, for example `fs-repo-11-to-12 --help`:
```
panic: qtls.ClientHelloInfo doesn't match
goroutine 1 [running]:
github.com/marten-seemann/qtls-go1-15.init.0()
github.com/marten-seemann/qtls-go1-15@v0.1.1/unsafe.go:20 +0x132
```
Also add myself as a maintainer for this package.
This fixes the test failure discovered in https://github.com/NixOS/nixpkgs/pull/160914.
See https://github.com/ipfs/fs-repo-migrations/issues/148 to read some of my struggles with updating this package.
2022-01-13 12:01:17 +00:00
$ { pkgs . ipfs-migrator } /bin/fs-repo-migrations - to ' $ { cfg . package . repoVersion } ' - y
2022-02-06 12:04:11 +00:00
'' + ''
ipfs - - offline config show \
| $ { pkgs . jq } /bin/jq ' . * $ extraConfig' - - argjson extraConfig $ {
2022-03-22 11:03:46 +00:00
escapeShellArg ( builtins . toJSON (
recursiveUpdate
{
Addresses . API = cfg . apiAddress ;
Addresses . Gateway = cfg . gatewayAddress ;
Addresses . Swarm = cfg . swarmAddress ;
}
cfg . extraConfig
) )
2022-02-06 12:04:11 +00:00
} \
| ipfs - - offline config replace -
'' ;
2020-06-11 20:27:22 +00:00
serviceConfig = {
2021-08-31 00:16:55 +00:00
ExecStart = [ " " " ${ cfg . package } / b i n / i p f s d a e m o n ${ ipfsFlags } " ] ;
2020-06-11 20:27:22 +00:00
User = cfg . user ;
Group = cfg . group ;
2022-03-17 15:40:54 +00:00
StateDirectory = " " ;
ReadWritePaths = [ " " cfg . dataDir ] ;
2020-06-11 20:27:22 +00:00
} // optionalAttrs ( cfg . serviceFdlimit != null ) { LimitNOFILE = cfg . serviceFdlimit ; } ;
2020-06-11 20:45:39 +00:00
} // optionalAttrs ( ! cfg . startWhenNeeded ) {
wantedBy = [ " d e f a u l t . t a r g e t " ] ;
} ;
2020-06-11 21:59:38 +00:00
systemd . sockets . ipfs-gateway = {
2020-06-11 20:45:39 +00:00
wantedBy = [ " s o c k e t s . t a r g e t " ] ;
2020-08-11 21:46:39 +00:00
socketConfig = {
2021-08-31 00:16:55 +00:00
ListenStream =
let
2020-08-11 21:46:39 +00:00
fromCfg = multiaddrToListenStream cfg . gatewayAddress ;
2021-08-31 00:16:55 +00:00
in
[ " " ] ++ lib . optional ( fromCfg != null ) fromCfg ;
ListenDatagram =
let
2020-08-11 21:46:39 +00:00
fromCfg = multiaddrToListenDatagram cfg . gatewayAddress ;
2021-08-31 00:16:55 +00:00
in
[ " " ] ++ lib . optional ( fromCfg != null ) fromCfg ;
2020-08-11 21:46:39 +00:00
} ;
2020-06-11 20:45:39 +00:00
} ;
2020-06-11 21:59:38 +00:00
systemd . sockets . ipfs-api = {
2020-06-11 20:45:39 +00:00
wantedBy = [ " s o c k e t s . t a r g e t " ] ;
2021-04-21 19:49:48 +00:00
# We also include "%t/ipfs.sock" because there is no way to put the "%t"
2020-06-17 20:19:01 +00:00
# in the multiaddr.
2021-08-31 00:16:55 +00:00
socketConfig . ListenStream =
let
2020-06-17 20:19:01 +00:00
fromCfg = multiaddrToListenStream cfg . apiAddress ;
2021-08-31 00:16:55 +00:00
in
[ " " " % t / i p f s . s o c k " ] ++ lib . optional ( fromCfg != null ) fromCfg ;
2017-08-12 13:40:05 +00:00
} ;
2022-04-15 19:06:21 +00:00
} ;
2017-08-12 13:40:05 +00:00
2022-04-15 19:06:21 +00:00
meta = {
maintainers = with lib . maintainers ; [ Luflosi ] ;
2016-10-30 19:39:53 +00:00
} ;
}