Created
March 6, 2017 08:57
-
-
Save manveru/8dc33e1e2fa28db16367b3d3d9ab5608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, lib, pkgs, ... }: | |
with lib; | |
let | |
cfg = config.services.syncthing-latest; | |
syncthing-source = pkgs.stdenv.mkDerivation rec { | |
name = "syncthing-source"; | |
version = "0.14.23"; | |
src = pkgs.fetchurl { | |
url = "https://github.com/syncthing/syncthing/releases/download/v${version}/syncthing-source-v${version}.tar.gz"; | |
sha256 = "10px05jz6arj2b76fw9cl7kcaq0gz10vf0p062mnl5lz9xxl1gkq"; | |
}; | |
phases = "unpackPhase buildPhase"; | |
buildInputs = [ pkgs.go ]; | |
unpackPhase = " | |
mkdir -p $out/gopath/src/github.com/syncthing | |
cd $out/gopath/src/github.com/syncthing | |
tar xf ${src} | |
"; | |
buildPhase = '' | |
cd $out/gopath/src/github.com/syncthing/syncthing | |
export GO15VENDOREXPERIMENT=1 | |
export GOPATH=$out/gopath | |
${pkgs.go}/bin/go run build.go -no-upgrade tar | |
mv $out/gopath/src/github.com/syncthing/syncthing/syncthing-*-v${version}.tar.gz $out/syncthing.tar.gz | |
rm -rf $out/gopath | |
''; | |
}; | |
syncthing-latest = pkgs.stdenv.mkDerivation rec { | |
name = "syncthing-latest"; | |
src = syncthing-source; | |
installPhase = '' | |
mkdir -p $out | |
cd $out | |
tar xf ${syncthing-source}/syncthing.tar.gz | |
dir=`echo syncthing-*-v${syncthing-source.version}-noupgrade` | |
echo $dir | |
mv syncthing-*-v${syncthing-source.version}-noupgrade/* . | |
rmdir syncthing-*-v${syncthing-source.version}-noupgrade/ | |
mkdir bin | |
mv syncthing bin | |
''; | |
}; | |
in { | |
###### interface | |
options = { | |
services.syncthing-latest = { | |
enable = mkOption { | |
default = true; | |
description = '' | |
Whether to enable the Syncthing, self-hosted open-source alternative | |
to Dropbox and BittorrentSync. Initial interface will be | |
available on http://127.0.0.1:8384/. | |
''; | |
}; | |
user = mkOption { | |
default = "syncthing"; | |
description = '' | |
Syncthing will be run under this user (user must exist, | |
this can be your user name). | |
''; | |
}; | |
dataDir = mkOption { | |
default = "/var/lib/syncthing"; | |
description = '' | |
Path where the settings and keys will exist. | |
''; | |
}; | |
package = mkOption { | |
type = types.package; | |
default = syncthing-latest; | |
defaultText = "pkgs.syncthing"; | |
example = literalExample "pkgs.syncthing"; | |
description = '' | |
Syncthing package to use. | |
''; | |
}; | |
}; | |
}; | |
###### implementation | |
config = mkIf cfg.enable { | |
systemd.services.syncthing-latest = { | |
description = "Syncthing service"; | |
after = [ "network.target" ]; | |
wantedBy = [ "multi-user.target" ]; | |
environment.STNORESTART = "yes"; # do not self-restart | |
environment.STNOUPGRADE = "yes"; | |
serviceConfig = { | |
User = "${cfg.user}"; | |
PermissionsStartOnly = true; | |
Restart = "on-failure"; | |
ExecStart = "${cfg.package}/bin/syncthing -no-browser -home=${cfg.dataDir}"; | |
SuccessExitStatus = "2 3 4"; | |
RestartForceExitStatus="3 4"; | |
}; | |
}; | |
environment.systemPackages = [ cfg.package ]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment