From 893b2de47b63dbeb0f759990f4f96165f1aefbaa Mon Sep 17 00:00:00 2001
From: Yusuf Bera Ertan <y.bera003.06@protonmail.com>
Date: Tue, 1 Dec 2020 00:15:18 +0300
Subject: [PATCH] nix: add .desktop file to veloren-voxygen derivation, update
 deps and other minor stuff

---
 nix/README.md    |  5 +++++
 nix/default.nix  | 37 ++++++++++++++++++++++---------------
 nix/shell.nix    |  2 +-
 nix/sources.json |  6 +++---
 4 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/nix/README.md b/nix/README.md
index 5ee0c69c30..3c38bd63d6 100644
--- a/nix/README.md
+++ b/nix/README.md
@@ -1,3 +1,8 @@
+## Important
+
+If you are going to call the derivations with a custom `nixpkgs` argument, make sure that the `nixpkgs` you pass is on at least the same commit or newer than it.
+Unexpected errors may pop up if you use an older version. Same goes for the `sources` argument.
+
 ### How to use
 
 To enter the development shell (which includes all tools mentioned in this readme + tools you'll need to develop Veloren), run:
diff --git a/nix/default.nix b/nix/default.nix
index b67f14c4e4..7400e85af1 100644
--- a/nix/default.nix
+++ b/nix/default.nix
@@ -67,11 +67,11 @@ let
     makeGitCommand "describe --exact-match --tags HEAD || printf ''"
       "getGitTag";
 
-  # If gitTag has a tag (meaning the commit we are on is a *release*), use it as version
-  # If not, we just use the prettified hash we have
+  # If gitTag has a tag (meaning the commit we are on is a *release*), use it as version, else:
+  # Just use the prettified hash we have, if we don't have it the build fails
   version = if gitTag != "" then gitTag else gitHash;
-  # Sanitize version string since it contains not allowed characters for a Nix store path
-  # Only used in the package name
+  # Sanitize version string since it might contain illegal characters for a Nix store path
+  # Used in the derivation(s) name
   sanitizedVersion = pkgs.stdenv.lib.strings.sanitizeDerivationName version;
 
   veloren-assets = pkgs.runCommand "makeAssetsDir" { } ''
@@ -79,26 +79,32 @@ let
     ln -sf ${../assets} $out/assets
   '';
 
+  velorenVoxygenDesktopFile = pkgs.makeDesktopItem rec {
+    name = "veloren-voxygen";
+    exec = name;
+    icon = ../assets/voxygen/logo.ico;
+    comment =
+      "Official client for Veloren - the open-world, open-source multiplayer voxel RPG";
+    desktopName = "Voxygen";
+    genericName = "Veloren Client";
+    categories = "Game;";
+  };
+
   veloren-crates = with pkgs;
     callPackage ./Cargo.nix {
       defaultCrateOverrides = with common;
         defaultCrateOverrides // {
-          libudev-sys = _: {
-            inherit (crateDeps.libudev-sys) nativeBuildInputs buildInputs;
-          };
-          alsa-sys = _: {
-            inherit (crateDeps.alsa-sys) nativeBuildInputs buildInputs;
-          };
+          libudev-sys = _: crateDeps.libudev-sys;
+          alsa-sys = _: crateDeps.alsa-sys;
+          veloren-network = _: crateDeps.veloren-network;
           veloren-common = _: {
             # Disable `git-lfs` check here since we check it ourselves
+            # We have to include the command output here, otherwise Nix won't run it
             DISABLE_GIT_LFS_CHECK = isGitLfsSetup;
             # Declare env values here so that `common/build.rs` sees them
             NIX_GIT_HASH = gitHash;
             NIX_GIT_TAG = gitTag;
           };
-          veloren-network = _: {
-            inherit (crateDeps.veloren-network) nativeBuildInputs buildInputs;
-          };
           veloren-server-cli = _: {
             name = "veloren-server-cli_${sanitizedVersion}";
             inherit version;
@@ -120,7 +126,8 @@ let
             VELOREN_USERDATA_STRATEGY = "system";
             inherit (crateDeps.veloren-voxygen) buildInputs;
             nativeBuildInputs = crateDeps.veloren-voxygen.nativeBuildInputs
-            ++ [ makeWrapper ];
+            ++ [ makeWrapper copyDesktopItems ];
+            desktopItems = [ velorenVoxygenDesktopFile ];
             postInstall = ''
               wrapProgram $out/bin/veloren-voxygen\
                 --set VELOREN_ASSETS ${veloren-assets}\
@@ -131,7 +138,7 @@ let
             meta = meta // {
               longDescription = ''
                 ${meta.longDescription}
-                "This package includes the client, Voxygen."
+                "This package includes the official client, Voxygen."
               '';
             };
           };
diff --git a/nix/shell.nix b/nix/shell.nix
index e35a8df71c..bc457a4ae7 100644
--- a/nix/shell.nix
+++ b/nix/shell.nix
@@ -16,7 +16,7 @@ let
 
   bundleCrate = writeScriptBin "bundleCrate" ''
     #!${stdenv.shell}
-    ${nix-bundle}/bin/nix-bundle "(pkgs.callPackage ./nix/default.nix { }).$1" /bin/$1
+    ${nix-bundle}/bin/nix-bundle "(pkgs.callPackage ./nix/default.nix { cratesToBuild = [ \"$1\" ]; }).$1" /bin/$1
   '';
 in
 with common;
diff --git a/nix/sources.json b/nix/sources.json
index ec43fbcabc..1e9d6794f1 100644
--- a/nix/sources.json
+++ b/nix/sources.json
@@ -29,10 +29,10 @@
         "homepage": "",
         "owner": "NixOS",
         "repo": "nixpkgs",
-        "rev": "1121b2259b7d66e8c7c5131d1588a48c80ef9e58",
-        "sha256": "0w2i4byhfn8c9lq8a97xnix5alfandqkbyvh6lbpr9zrm63lmyip",
+        "rev": "b6bca3d80619f1565ba0ea635b0d38234e41c6bd",
+        "sha256": "09d4f6h98rmxnxzm1x07jxgrc81k6mz7fjigq375fkmb41j2kdsi",
         "type": "tarball",
-        "url": "https://github.com/NixOS/nixpkgs/archive/1121b2259b7d66e8c7c5131d1588a48c80ef9e58.tar.gz",
+        "url": "https://github.com/NixOS/nixpkgs/archive/b6bca3d80619f1565ba0ea635b0d38234e41c6bd.tar.gz",
         "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
     },
     "nixpkgsMoz": {