···1+{ stdenv, callPackage, fetchurl, makeWrapper
2+# Begin libraries
3+, alsaLib, libX11, libXcursor, libXinerama, libXrandr, libXi
4+# Begin download parameters
5+, username ? ""
6+, password ? ""
7+}:
8+9+let
10+ version = "0.12.20";
11+12+ fetch = callPackage ./fetch.nix { username = username; password = password; };
13+ arch = if stdenv.system == "x86_64-linux" then "x64"
14+ else if stdenv.system == "i686-linux" then "x32"
15+ else abort "Unsupported platform";
16+17+ variants = {
18+ x64 = {
19+ url = "https://www.factorio.com/get-download/${version}/alpha/linux64";
20+ sha256 = "1xpzrx3q678519qgjl92fxn3qv55hd188x9jp6dcfk2ljhi1gmqk";
21+ };
22+23+ x32 = {
24+ url = "https://www.factorio.com/get-download/${version}/alpha/linux32";
25+ sha256 = "1dl1dsp4nni5nda437ckyw1ss6w168g19v51h7cdvb3cgsdb7sab";
26+ };
27+ };
28+in
29+30+stdenv.mkDerivation rec {
31+ name = "factorio-${version}";
32+33+ src = fetch variants.${arch};
34+35+ libPath = stdenv.lib.makeLibraryPath [
36+ alsaLib
37+ libX11
38+ libXcursor
39+ libXinerama
40+ libXrandr
41+ libXi
42+ ];
43+44+ buildInputs = [ makeWrapper ];
45+46+ installPhase = ''
47+ mkdir -p $out/{bin,share/factorio}
48+ cp -a bin/${arch}/factorio $out/bin/factorio.${arch}
49+ cp -a doc-html data $out/share/factorio/
50+51+ # Fortunately, Factorio already supports system-wide installs.
52+ # Unfortunately it's a bit inconvenient to set the paths.
53+ cat > $out/share/factorio/config-base.cfg <<EOF
54+use-system-read-write-data-directories=false
55+[path]
56+read-data=$out/share/factorio/data/
57+EOF
58+59+ cat > $out/share/factorio/update-config.sh <<EOF
60+if [[ -e ~/.factorio/config.cfg ]]; then
61+ # Config file exists, but may have wrong path.
62+ # Try to edit it. I'm sure this is perfectly safe and will never go wrong.
63+ sed -i 's|^read-data=.*|read-data=$out/share/factorio/data/|' ~/.factorio/config.cfg
64+else
65+ # Config file does not exist. Phew.
66+ install -D $out/share/factorio/config-base.cfg ~/.factorio/config.cfg
67+fi
68+EOF
69+ chmod a+x $out/share/factorio/update-config.sh
70+71+ patchelf \
72+ --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \
73+ $out/bin/factorio.${arch}
74+75+ makeWrapper $out/bin/factorio.${arch} $out/bin/factorio \
76+ --prefix LD_LIBRARY_PATH : /run/opengl-driver/lib:$libPath \
77+ --run "$out/share/factorio/update-config.sh" \
78+ --add-flags "-c \$HOME/.factorio/config.cfg"
79+ '';
80+81+ meta = {
82+ description = "A game in which you build and maintain factories.";
83+ longDescription = ''
84+ Factorio is a game in which you build and maintain factories.
85+86+ You will be mining resources, researching technologies, building
87+ infrastructure, automating production and fighting enemies. Use your
88+ imagination to design your factory, combine simple elements into
89+ ingenious structures, apply management skills to keep it working and
90+ finally protect it from the creatures who don't really like you.
91+92+ Factorio has been in development since spring of 2012 and it is
93+ currently in late alpha.
94+ '';
95+ homepage = https://www.factorio.com/;
96+ license = stdenv.lib.licenses.unfree;
97+ maintainers = [ stdenv.lib.maintainers.Baughn ];
98+ platforms = [ "i686-linux" "x86_64-linux" ];
99+ };
100+}
+33
pkgs/games/factorio/fetch.nix
···000000000000000000000000000000000
···1+{ stdenv, curl
2+# Begin download parameters
3+, username ? ""
4+, password ? ""
5+}:
6+7+{
8+ # URL to fetch.
9+ url ? ""
10+11+ # Login URL.
12+, loginUrl ? "https://www.factorio.com/login"
13+14+ # SHA256 of the fetched URL.
15+, sha256 ? ""
16+}:
17+18+stdenv.mkDerivation {
19+ name = "factorio.tar.gz";
20+21+ buildInputs = [ curl ];
22+23+ inherit url loginUrl username password;
24+25+ builder = ./fetch.sh;
26+27+ outputHashAlgo = "sha256";
28+ outputHash = sha256;
29+ outputHashMode = "flat";
30+31+ # There's no point in downloading remotely, we'd just slow things down.
32+ preferLocalBuild = true;
33+}
+44
pkgs/games/factorio/fetch.sh
···00000000000000000000000000000000000000000000
···1+source $stdenv/setup
2+3+# Curl flags to increase reliability a bit.
4+#
5+# Can't use fetchurl, for several reasons. One is that we definitely
6+# don't want --insecure for the login, though we need it for the
7+# download as their download cert isn't in the standard linux bundle.
8+curl="curl \
9+ --max-redirs 20 \
10+ --retry 3 \
11+ --cacert /etc/ssl/certs/ca-bundle.crt \
12+ $curlOpts \
13+ $NIX_CURL_FLAGS"
14+15+# We don't want the password to be on any program's argv, as it may be
16+# visible in /proc. Writing it to file with echo should be safe, since
17+# it's a shell builtin.
18+echo "password=$password" > password
19+# Might as well hide the username as well.
20+echo "username-or-email=$username" > username
21+22+# Log in. We don't especially care about the result, but let's check if login failed.
23+$curl -c cookies -d @username -d @password $loginUrl -D headers > /dev/null
24+25+if grep -q 'Location: /' headers; then
26+ # Now download. We need --insecure for this, but the sha256 should cover us.
27+ $curl -b cookies --insecure --location $url > $out
28+else
29+ echo 'Login failed'
30+ echo 'Please set username and password with config.nix,'
31+ echo 'or /etc/nix/nixpkgs-config.nix if on NixOS.'
32+ echo
33+ echo 'Example:'
34+ echo '{'
35+ echo ' packageOverrides = pkgs: rec {'
36+ echo ' factorio = pkgs.factorio.override {'
37+ echo ' username = "<username or email address>";'
38+ echo ' password = "<password>";'
39+ echo ' };'
40+ echo ' };'
41+ echo '}'
42+43+ exit 1
44+fi