···11+{ stdenv, callPackage, fetchurl, makeWrapper
22+# Begin libraries
33+, alsaLib, libX11, libXcursor, libXinerama, libXrandr, libXi
44+# Begin download parameters
55+, username ? ""
66+, password ? ""
77+}:
88+99+let
1010+ version = "0.12.20";
1111+1212+ fetch = callPackage ./fetch.nix { username = username; password = password; };
1313+ arch = if stdenv.system == "x86_64-linux" then "x64"
1414+ else if stdenv.system == "i686-linux" then "x32"
1515+ else abort "Unsupported platform";
1616+1717+ variants = {
1818+ x64 = {
1919+ url = "https://www.factorio.com/get-download/${version}/alpha/linux64";
2020+ sha256 = "1xpzrx3q678519qgjl92fxn3qv55hd188x9jp6dcfk2ljhi1gmqk";
2121+ };
2222+2323+ x32 = {
2424+ url = "https://www.factorio.com/get-download/${version}/alpha/linux32";
2525+ sha256 = "1dl1dsp4nni5nda437ckyw1ss6w168g19v51h7cdvb3cgsdb7sab";
2626+ };
2727+ };
2828+in
2929+3030+stdenv.mkDerivation rec {
3131+ name = "factorio-${version}";
3232+3333+ src = fetch variants.${arch};
3434+3535+ libPath = stdenv.lib.makeLibraryPath [
3636+ alsaLib
3737+ libX11
3838+ libXcursor
3939+ libXinerama
4040+ libXrandr
4141+ libXi
4242+ ];
4343+4444+ buildInputs = [ makeWrapper ];
4545+4646+ installPhase = ''
4747+ mkdir -p $out/{bin,share/factorio}
4848+ cp -a bin/${arch}/factorio $out/bin/factorio.${arch}
4949+ cp -a doc-html data $out/share/factorio/
5050+5151+ # Fortunately, Factorio already supports system-wide installs.
5252+ # Unfortunately it's a bit inconvenient to set the paths.
5353+ cat > $out/share/factorio/config-base.cfg <<EOF
5454+use-system-read-write-data-directories=false
5555+[path]
5656+read-data=$out/share/factorio/data/
5757+EOF
5858+5959+ cat > $out/share/factorio/update-config.sh <<EOF
6060+if [[ -e ~/.factorio/config.cfg ]]; then
6161+ # Config file exists, but may have wrong path.
6262+ # Try to edit it. I'm sure this is perfectly safe and will never go wrong.
6363+ sed -i 's|^read-data=.*|read-data=$out/share/factorio/data/|' ~/.factorio/config.cfg
6464+else
6565+ # Config file does not exist. Phew.
6666+ install -D $out/share/factorio/config-base.cfg ~/.factorio/config.cfg
6767+fi
6868+EOF
6969+ chmod a+x $out/share/factorio/update-config.sh
7070+7171+ patchelf \
7272+ --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \
7373+ $out/bin/factorio.${arch}
7474+7575+ makeWrapper $out/bin/factorio.${arch} $out/bin/factorio \
7676+ --prefix LD_LIBRARY_PATH : /run/opengl-driver/lib:$libPath \
7777+ --run "$out/share/factorio/update-config.sh" \
7878+ --add-flags "-c \$HOME/.factorio/config.cfg"
7979+ '';
8080+8181+ meta = {
8282+ description = "A game in which you build and maintain factories.";
8383+ longDescription = ''
8484+ Factorio is a game in which you build and maintain factories.
8585+8686+ You will be mining resources, researching technologies, building
8787+ infrastructure, automating production and fighting enemies. Use your
8888+ imagination to design your factory, combine simple elements into
8989+ ingenious structures, apply management skills to keep it working and
9090+ finally protect it from the creatures who don't really like you.
9191+9292+ Factorio has been in development since spring of 2012 and it is
9393+ currently in late alpha.
9494+ '';
9595+ homepage = https://www.factorio.com/;
9696+ license = stdenv.lib.licenses.unfree;
9797+ maintainers = [ stdenv.lib.maintainers.Baughn ];
9898+ platforms = [ "i686-linux" "x86_64-linux" ];
9999+ };
100100+}
+33
pkgs/games/factorio/fetch.nix
···11+{ stdenv, curl
22+# Begin download parameters
33+, username ? ""
44+, password ? ""
55+}:
66+77+{
88+ # URL to fetch.
99+ url ? ""
1010+1111+ # Login URL.
1212+, loginUrl ? "https://www.factorio.com/login"
1313+1414+ # SHA256 of the fetched URL.
1515+, sha256 ? ""
1616+}:
1717+1818+stdenv.mkDerivation {
1919+ name = "factorio.tar.gz";
2020+2121+ buildInputs = [ curl ];
2222+2323+ inherit url loginUrl username password;
2424+2525+ builder = ./fetch.sh;
2626+2727+ outputHashAlgo = "sha256";
2828+ outputHash = sha256;
2929+ outputHashMode = "flat";
3030+3131+ # There's no point in downloading remotely, we'd just slow things down.
3232+ preferLocalBuild = true;
3333+}
+44
pkgs/games/factorio/fetch.sh
···11+source $stdenv/setup
22+33+# Curl flags to increase reliability a bit.
44+#
55+# Can't use fetchurl, for several reasons. One is that we definitely
66+# don't want --insecure for the login, though we need it for the
77+# download as their download cert isn't in the standard linux bundle.
88+curl="curl \
99+ --max-redirs 20 \
1010+ --retry 3 \
1111+ --cacert /etc/ssl/certs/ca-bundle.crt \
1212+ $curlOpts \
1313+ $NIX_CURL_FLAGS"
1414+1515+# We don't want the password to be on any program's argv, as it may be
1616+# visible in /proc. Writing it to file with echo should be safe, since
1717+# it's a shell builtin.
1818+echo "password=$password" > password
1919+# Might as well hide the username as well.
2020+echo "username-or-email=$username" > username
2121+2222+# Log in. We don't especially care about the result, but let's check if login failed.
2323+$curl -c cookies -d @username -d @password $loginUrl -D headers > /dev/null
2424+2525+if grep -q 'Location: /' headers; then
2626+ # Now download. We need --insecure for this, but the sha256 should cover us.
2727+ $curl -b cookies --insecure --location $url > $out
2828+else
2929+ echo 'Login failed'
3030+ echo 'Please set username and password with config.nix,'
3131+ echo 'or /etc/nix/nixpkgs-config.nix if on NixOS.'
3232+ echo
3333+ echo 'Example:'
3434+ echo '{'
3535+ echo ' packageOverrides = pkgs: rec {'
3636+ echo ' factorio = pkgs.factorio.override {'
3737+ echo ' username = "<username or email address>";'
3838+ echo ' password = "<password>";'
3939+ echo ' };'
4040+ echo ' };'
4141+ echo '}'
4242+4343+ exit 1
4444+fi