From 84a7f666c2b4bbc8fa62b78a2d6fb8268c1ba1d3 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Fri, 16 Apr 2021 18:09:51 +0300 Subject: [PATCH] build(nix): switch to nix-cargo-integration --- Cargo.toml | 10 ++++ flake.lock | 58 ++++++++++++++++---- flake.nix | 120 +++++++++++++++++++++++++++--------------- nix/build.nix | 118 ----------------------------------------- nix/common.nix | 34 ------------ nix/devShell.nix | 25 --------- nix/envrc | 2 - server-cli/Cargo.toml | 10 ++++ voxygen/Cargo.toml | 11 ++++ 9 files changed, 157 insertions(+), 231 deletions(-) delete mode 100644 nix/build.nix delete mode 100644 nix/common.nix delete mode 100644 nix/devShell.nix diff --git a/Cargo.toml b/Cargo.toml index 4ef02f3feb..3a975c6c65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,3 +96,13 @@ debug = 1 # macos CI fix isn't merged yet winit = { git = "https://gitlab.com/veloren/winit.git", branch = "macos-test-spiffed" } vek = { git = "https://gitlab.com/veloren/vek.git", branch = "fix_intrinsics2" } + +[workspace.metadata.nix] +systems = ["x86_64-linux"] +buildInputs = ["libudev", "alsaLib", "openssl", "xorg.libxcb", "libxkbcommon"] +nativeBuildInputs = ["pkg-config", "python3", "binutils"] +runtimeLibs = ["libGL", "xorg.libX11", "xorg.libXcursor", "xorg.libXrandr", "xorg.libXi", "wayland", "wayland-protocols"] + +[workspace.metadata.nix.cachix] +name = "veloren-nix" +key = "veloren-nix.cachix.org-1:zokfKJqVsNV6kI/oJdLF6TYBdNPYGSb+diMVQPn/5Rc=" diff --git a/flake.lock b/flake.lock index c221ca1ffc..640c2acb74 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "devshell": { + "locked": { + "lastModified": 1618213758, + "narHash": "sha256-qkuen2xlT7gVfX+99OeM4lsKXGjT5EDjytWAeAWX0Fw=", + "owner": "numtide", + "repo": "devshell", + "rev": "eac56f015b0975be3b725721503626e926b285e8", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, "flake-utils": { "locked": { "lastModified": 1614513358, @@ -37,11 +52,11 @@ ] }, "locked": { - "lastModified": 1616263753, - "narHash": "sha256-wf1naH44D0Z7+JNpD6T/6/d0XaXDi9r4Oi100pBtzhs=", + "lastModified": 1618579991, + "narHash": "sha256-aIWvSjn/rjfOBUdL5uM4iDVVLM7ioRz9zs3e94lT9Ac=", "owner": "yusdacra", "repo": "naersk", - "rev": "8cd4d175606de222114c551c85847acb013cc9cb", + "rev": "dc8868af09c8a0f0d47df0927161cb14675c5534", "type": "github" }, "original": { @@ -51,13 +66,39 @@ "type": "github" } }, + "nixCargoIntegration": { + "inputs": { + "devshell": "devshell", + "flakeUtils": "flakeUtils", + "naersk": [ + "naersk" + ], + "nixpkgs": [ + "nixpkgs" + ], + "rustOverlay": "rustOverlay" + }, + "locked": { + "lastModified": 1618597947, + "narHash": "sha256-wiwHpQPzgkuOEZXFpet7bmv6Z8dusxyp6DQbpYCXJDk=", + "owner": "yusdacra", + "repo": "nix-cargo-integration", + "rev": "fc26a4e1d807e8b9722d67097be4dfb8be5ea862", + "type": "github" + }, + "original": { + "owner": "yusdacra", + "repo": "nix-cargo-integration", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1618306168, - "narHash": "sha256-/IOtgzdEaiEqRVn6OhpekuYhRHfCm299Bqts5QqQVko=", + "lastModified": 1618530127, + "narHash": "sha256-jQPk7XpabppYa+GIO5LVTtUFhHq/VqHhPggwIKhTCnQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "4c87cb87a2db6b9eb43541c1cf83f2a2f725fa25", + "rev": "294d1925af6462e55c76b49624b983036f0093b9", "type": "github" }, "original": { @@ -69,10 +110,9 @@ }, "root": { "inputs": { - "flakeUtils": "flakeUtils", "naersk": "naersk", - "nixpkgs": "nixpkgs", - "rustOverlay": "rustOverlay" + "nixCargoIntegration": "nixCargoIntegration", + "nixpkgs": "nixpkgs" } }, "rustOverlay": { diff --git a/flake.nix b/flake.nix index a3da8209af..e3608e0a34 100644 --- a/flake.nix +++ b/flake.nix @@ -6,56 +6,90 @@ url = "github:yusdacra/naersk/veloren"; inputs.nixpkgs.follows = "nixpkgs"; }; - flakeUtils.url = "github:numtide/flake-utils"; - rustOverlay = { - url = "github:oxalica/rust-overlay"; - inputs.nixpkgs.follows = "nixpkgs"; - }; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nixCargoIntegration = { + url = "github:yusdacra/nix-cargo-integration"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.naersk.follows = "naersk"; + }; }; - outputs = inputs: with inputs; with flakeUtils.lib; - eachSystem [ "x86_64-linux" ] (system: - let - common = import ./nix/common.nix { - sources = { - inherit - naersk - nixpkgs - rustOverlay - ; - }; - inherit system; - }; - - mkPackage = crateName: import ./nix/build.nix { - inherit common; - crateToBuild = crateName; + outputs = inputs: inputs.nixCargoIntegration.lib.makeOutputs { + root = ./.; + overrides = { + common = prev: { + gitLfsCheckFile = ./assets/voxygen/background/bg_main.png; + utils = import ./nix/utils.nix { pkgs = prev.pkgs; }; + }; + build = common: prevb: + let + pkgs = common.pkgs; sourceInfo = - if self.sourceInfo ? rev - then self.sourceInfo // { + if inputs.self.sourceInfo ? rev + then inputs.self.sourceInfo // { # Tag would have to be set manually for stable releases flake # because there's currently no way to get the tag via the interface. - # tag = v0.8.0; + # tag = v0.9.0; } else (throw "Can't get revision because the git tree is dirty"); + + prettyRev = with sourceInfo; builtins.substring 0 8 rev + "/" + common.utils.dateTimeFormat lastModified; + + tag = with sourceInfo; + if sourceInfo ? tag + then sourceInfo.tag + else ""; + + # 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. + # Must be in format f4987672/2020-12-10-12:00 + version = + if tag != "" then tag + else if prettyRev != "" then prettyRev + else throw "Need a tag or pretty revision in order to determine version"; + + veloren-assets = pkgs.runCommand "makeAssetsDir" { } '' + mkdir $out + ln -sf ${./assets} $out/assets + ''; + + velorenOverride = oldAttr: + if common.cargoPkg.name == "veloren-voxygen" + then + { + nativeBuildInputs = oldAttr.nativeBuildInputs ++ [ pkgs.makeWrapper ]; + postInstall = '' + wrapProgram $out/bin/veloren-voxygen\ + --set VELOREN_ASSETS ${veloren-assets}\ + --set LD_LIBRARY_PATH ${ + pkgs.lib.makeLibraryPath common.runtimeLibs + } + ''; + } + else if common.cargoPkg.name == "veloren-server-cli" + then + { + nativeBuildInputs = oldAttr.nativeBuildInputs ++ [ pkgs.makeWrapper ]; + postInstall = '' + wrapProgram $out/bin/veloren-server-cli --set VELOREN_ASSETS ${veloren-assets} + ''; + } + else { }; + in + { + allRefs = true; + override = old: (prevb.override old) // { + # 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 = common.utils.isGitLfsSetup common.gitLfsCheckFile; + # Declare env values here so that `common/build.rs` sees them + NIX_GIT_HASH = prettyRev; + NIX_GIT_TAG = tag; + VELOREN_USERDATA_STRATEGY = "system"; + }; + overrideMain = old: (prevb.overrideMain old) // (velorenOverride old); }; - - cratesToPackage = [ "veloren-voxygen" "veloren-server-cli" ]; - - genAttrs = names: f: - builtins.listToAttrs (map (n: { name = n; value = f n; }) names); - in - rec { - packages = genAttrs cratesToPackage mkPackage; - defaultPackage = packages.veloren-voxygen; - - apps = builtins.mapAttrs (n: v: mkApp { name = n; drv = v; }) packages; - defaultApp = apps.veloren-voxygen; - - devShell = import ./nix/devShell.nix { - inherit common; - }; - } - ); + }; + }; } diff --git a/nix/build.nix b/nix/build.nix deleted file mode 100644 index ef83e385dd..0000000000 --- a/nix/build.nix +++ /dev/null @@ -1,118 +0,0 @@ -{ common -, release ? true -, crateToBuild ? "veloren-voxygen" -, disableGitLfsCheck ? false -, sourceInfo ? { } -}: -with common; -let - utils = import ./utils.nix { inherit pkgs; }; - - meta = with pkgs.lib; { - description = "Veloren is a multiplayer voxel RPG written in Rust."; - longDescription = '' - Veloren is a multiplayer voxel RPG written in Rust. - It is inspired by games such as Cube World, Legend of Zelda: Breath of the Wild, Dwarf Fortress and Minecraft. - ''; - homepage = "https://veloren.net"; - license = licenses.gpl3; - maintainers = [ maintainers.yusdacra ]; - # TODO: Make this work on BSD and Mac OS - platforms = platforms.linux; - }; - - prettyRev = with sourceInfo; builtins.substring 0 8 rev + "/" + utils.dateTimeFormat lastModified; - - tag = with sourceInfo; - if sourceInfo ? tag - then sourceInfo.tag - else ""; - - # 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. - # Must be in format f4987672/2020-12-10-12:00 - version = - if tag != "" then tag - else if prettyRev != "" then prettyRev - else throw "Need a tag or pretty revision in order to determine version"; - - veloren-assets = pkgs.runCommand "makeAssetsDir" { } '' - mkdir $out - 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;"; - }; - - velorenOverride = oldAttr: with pkgs; - if crateToBuild == "veloren-voxygen" - then - { - pname = "veloren-voxygen"; - inherit version; - - nativeBuildInputs = (oldAttr.nativeBuildInputs or [ ]) ++ [ makeWrapper copyDesktopItems ]; - desktopItems = [ velorenVoxygenDesktopFile ]; - postInstall = '' - wrapProgram $out/bin/veloren-voxygen\ - --set VELOREN_ASSETS ${veloren-assets}\ - --set LD_LIBRARY_PATH ${ - lib.makeLibraryPath common.voxygenNeededLibs - } - ''; - meta = meta // { - longDescription = '' - ${meta.longDescription} - "This package includes the official client, Voxygen." - ''; - }; - } - else if crateToBuild == "veloren-server-cli" - then - { - pname = "veloren-server-cli"; - inherit version; - - nativeBuildInputs = (oldAttr.nativeBuildInputs or [ ]) ++ [ makeWrapper ]; - postInstall = '' - wrapProgram $out/bin/veloren-server-cli --set VELOREN_ASSETS ${veloren-assets} - ''; - meta = meta // { - longDescription = '' - ${meta.longDescription} - "This package includes the server CLI." - ''; - }; - } - else { }; - - velorenPackage = - pkgs.naersk.buildPackage { - name = crateToBuild; - root = ../.; - targets = [ crateToBuild ]; - nativeBuildInputs = common.crateDeps.nativeBuildInputs; - buildInputs = common.crateDeps.buildInputs; - override = (_: { - # 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 = utils.isGitLfsSetup common.gitLfsCheckFile; - # Declare env values here so that `common/build.rs` sees them - NIX_GIT_HASH = prettyRev; - NIX_GIT_TAG = tag; - VELOREN_USERDATA_STRATEGY = "system"; - }); - overrideMain = velorenOverride; - inherit release; - }; -in -velorenPackage diff --git a/nix/common.nix b/nix/common.nix deleted file mode 100644 index bdc417469b..0000000000 --- a/nix/common.nix +++ /dev/null @@ -1,34 +0,0 @@ -{ sources, system }: -let - pkgz = import sources.nixpkgs { inherit system; overlays = [ sources.rustOverlay.overlay ]; }; - rust = (pkgz.rust-bin.fromRustupToolchainFile ../rust-toolchain).override { - extensions = [ "rust-src" ]; - }; - - pkgs = import sources.nixpkgs { - inherit system; - overlays = [ - (final: prev: { - rustc = rust; - }) - (final: prev: { - naersk = prev.callPackage sources.naersk { }; - }) - ]; - }; -in -with pkgs; -{ - inherit pkgs; - # deps that crates need (for compiling) - crateDeps = - { - nativeBuildInputs = [ pkg-config python3 ]; - buildInputs = [ libudev alsaLib openssl xorg.libxcb ]; - }; - # deps that voxygen needs to function - # FIXME: Wayland doesn't work (adding libxkbcommon, wayland and wayland-protocols results in a panic) - voxygenNeededLibs = (with xorg; [ libX11 libXcursor libXrandr libXi ]) - ++ [ libGL ]; - gitLfsCheckFile = ../assets/voxygen/background/bg_main.png; -} diff --git a/nix/devShell.nix b/nix/devShell.nix deleted file mode 100644 index 7a17c3aaa4..0000000000 --- a/nix/devShell.nix +++ /dev/null @@ -1,25 +0,0 @@ -{ common }: -with common; with pkgs; -mkShell { - name = "veloren-shell"; - nativeBuildInputs = [ - cachix - git - git-lfs - nixpkgs-fmt - rustc - ] ++ crateDeps.nativeBuildInputs; - buildInputs = crateDeps.buildInputs; - shellHook = '' - # Setup our cachix "substituter" - export NIX_CONFIG=" - substituters = https://cache.nixos.org https://veloren-nix.cachix.org - trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= veloren-nix.cachix.org-1:zokfKJqVsNV6kI/oJdLF6TYBdNPYGSb+diMVQPn/5Rc= - " - # Add libraries Voxygen needs so that it runs - export LD_LIBRARY_PATH=${lib.makeLibraryPath voxygenNeededLibs} - - # No need to install git-lfs and run fetch / checkout commands if we have it setup - [ "$(${file}/bin/file --mime-type ${gitLfsCheckFile})" = "${gitLfsCheckFile}: image/png" ] || (git lfs install --local && git lfs fetch && git lfs checkout) - ''; -} diff --git a/nix/envrc b/nix/envrc index c258f265aa..9777841235 100644 --- a/nix/envrc +++ b/nix/envrc @@ -1,7 +1,5 @@ watch_file flake.lock watch_file flake.nix -watch_file nix/common.nix -watch_file nix/devShell.nix watch_file nix/shell.nix watch_file nix/utils.nix diff --git a/server-cli/Cargo.toml b/server-cli/Cargo.toml index 3fa9f0c894..6041be5e66 100644 --- a/server-cli/Cargo.toml +++ b/server-cli/Cargo.toml @@ -4,6 +4,16 @@ version = "0.9.0" authors = ["Joshua Barretto "] edition = "2018" +[package.metadata.nix] +build = true +app = true +longDescription = """ +Veloren is a multiplayer voxel RPG written in Rust. +It is inspired by games such as Cube World, Legend of Zelda: Breath of the Wild, Dwarf Fortress and Minecraft. + +This package includes the official server CLI. +""" + [features] worldgen = ["server/worldgen"] default = ["worldgen"] diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index 5094d86d61..abf119b046 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -8,6 +8,17 @@ version = "0.9.0" # Uncomment below and comment out default-run if you want to avoid this # autobins = false +[package.metadata.nix] +build = true +app = true +desktopFile = "./assets/voxygen/net.veloren.veloren.desktop" +longDescription = """ +Veloren is a multiplayer voxel RPG written in Rust. +It is inspired by games such as Cube World, Legend of Zelda: Breath of the Wild, Dwarf Fortress and Minecraft. + +This package includes the official client, Voxygen. +""" + [features] gl = ["gfx_device_gl", "gfx_gl"] hot-anim = ["anim/use-dyn-lib"]