From 4437c10fad8925d6322c83bf1553daa9a36ceb31 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Sat, 2 Jan 2021 14:06:16 -0500 Subject: nix, work on build --- nix/dub.selections.nix | 50 ++++++++++++++++++++ nix/mkDub.nix | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ nix/shell-packages.nix | 15 ++++++ 3 files changed, 186 insertions(+) create mode 100644 nix/dub.selections.nix create mode 100644 nix/mkDub.nix create mode 100644 nix/shell-packages.nix (limited to 'nix') diff --git a/nix/dub.selections.nix b/nix/dub.selections.nix new file mode 100644 index 0000000..28f9d2e --- /dev/null +++ b/nix/dub.selections.nix @@ -0,0 +1,50 @@ +# This file was generated by https://github.com/lionello/dub2nix v0.2.3 +[ { + fetch = { + type = "git"; + url = "https://github.com/dlang-community/d2sqlite3.git"; + rev = "v0.19.1"; + sha256 = "0rnsgla6xyr8r34knf7v6dwhacra96q1b5rhxcz9246inwhvrk5k"; + fetchSubmodules = false; + date = "2020-07-21T12:32:51+02:00"; + deepClone = false; + leaveDotGit = false; + path = "/nix/store/hsi8xvl15w6fwlqvs042m1z5i88yc72i-d2sqlite3"; + }; +} { + fetch = { + type = "git"; + url = "https://github.com/kiith-sa/tinyendian.git"; + rev = "v0.2.0"; + sha256 = "086gf5aga52wr5rj2paq54daj8lafn980x77b706vvvqaz2mlis8"; + fetchSubmodules = false; + date = "2018-06-10T11:04:28+02:00"; + deepClone = false; + leaveDotGit = false; + path = "/nix/store/9c7fsmi5am84j6dq2mp3va306x3ay291-tinyendian"; + }; +} { + fetch = { + type = "git"; + url = "https://github.com/kiith-sa/D-YAML.git"; + rev = "v0.8.3"; + sha256 = "13wy304xjbwkpgg7ilql1lkxkm83s87jm59ffnrg26slp7cx149q"; + fetchSubmodules = false; + date = "2020-09-19T23:46:57+02:00"; + deepClone = false; + leaveDotGit = false; + path = "/nix/store/3i8i56lkmw2xq3lxr5h66v909waq2mqg-D-YAML"; + }; +} { + fetch = { + type = "git"; + url = "https://github.com/lgvz/imageformats.git"; + rev = "v7.0.2"; + sha256 = "1mfbsmi4fs1xny4zqs6jyr04d5f4h03r9f6jadvkdqj5kd1k0ws7"; + fetchSubmodules = false; + date = "2019-10-10T07:54:45+03:00"; + deepClone = false; + leaveDotGit = false; + path = "/nix/store/wn554pn21nzmpvw2hs7hvv9v9y0sgarg-imageformats"; + }; +} ] diff --git a/nix/mkDub.nix b/nix/mkDub.nix new file mode 100644 index 0000000..bfcb038 --- /dev/null +++ b/nix/mkDub.nix @@ -0,0 +1,121 @@ +{ pkgs ? import {}, + stdenv ? pkgs.stdenv, + ldc ? pkgs.ldc, + dub ? pkgs.dub +}: + +with stdenv; +let + # Filter function to remove the .dub package folder from src + filterDub = name: type: let baseName = baseNameOf (toString name); in ! ( + type == "directory" && baseName == ".dub" + ); + + # Convert a GIT rev string (tag) to a simple semver version + rev-to-version = builtins.replaceStrings ["v" "refs/tags/v"] ["" ""]; + + dep2src = dubDep: pkgs.fetchgit { inherit (dubDep.fetch) url rev sha256 fetchSubmodules; }; + + # Fetch a dependency (source only for now) + fromDub = dubDep: mkDerivation rec { + name = "${src.name}-${version}"; + version = rev-to-version dubDep.fetch.rev; + nativeBuildInputs = [ ldc dub ]; + src = dep2src dubDep; + + buildPhase = '' + runHook preBuild + export HOME=$PWD + dub build -b=release + runHook postBuild + ''; + + # outputs = [ "lib" ]; + + # installPhase = '' + # runHook preInstall + # mkdir -p $out/bin + # runHook postInstall + # ''; + }; + + # Adds a local package directory (e.g. a git repository) to Dub + dub-add-local = dubDep: "dub add-local ${(fromDub dubDep).src.outPath} ${rev-to-version dubDep.fetch.rev}"; + + # The target output of the Dub package + targetOf = package: "${package.targetPath or "."}/${package.targetName or package.name}"; + + # Remove reference to build tools and library sources + disallowedReferences = deps: [ ldc dub ] ++ builtins.map dep2src deps; + + removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}''; + +in { + inherit fromDub; + + mkDubDerivation = lib.makeOverridable ({ + src, + nativeBuildInputs ? [], + dubJSON ? src + "/dub.json", + selections ? src + "/nix/dub.selections.nix", + deps ? import selections, + passthru ? {}, + package ? lib.importJSON dubJSON, + ... + } @ attrs: stdenv.mkDerivation (attrs // { + + pname = package.name; + + nativeBuildInputs = [ ldc dub pkgs.removeReferencesTo ] ++ nativeBuildInputs; + disallowedReferences = disallowedReferences deps; + + passthru = passthru // { + inherit dub ldc pkgs; + }; + + src = lib.cleanSourceWith { + filter = filterDub; + src = lib.cleanSource src; + }; + + preFixup = '' + find $out/bin -type f -exec ${removeExpr (disallowedReferences deps)} '{}' + || true + ''; + + buildPhase = '' + runHook preBuild + + export HOME=$PWD + ${lib.concatMapStringsSep "\n" dub-add-local deps} + dub build --compiler=ldc2 --build=release --combined --skip-registry=all + + runHook postBuild + ''; + + checkPhase = '' + runHook preCheck + + export HOME=$PWD + ${lib.concatMapStringsSep "\n" dub-add-local deps} + dub test --combined --skip-registry=all + + runHook postCheck + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + cp -r "${targetOf package}" $out/bin + + runHook postInstall + ''; + + meta = lib.optionalAttrs (package ? description) { + description = package.description; + } // attrs.meta or {}; + } // lib.optionalAttrs (!(attrs ? version)) { + # Use name from dub.json, unless pname and version are specified + name = package.name; + })); +} diff --git a/nix/shell-packages.nix b/nix/shell-packages.nix new file mode 100644 index 0000000..9e40e24 --- /dev/null +++ b/nix/shell-packages.nix @@ -0,0 +1,15 @@ +{ pkgs ? import {} }: +let + dub2nix-src = fetchTarball { + url = "https://github.com/lionello/dub2nix/archive/master.tar.gz"; + }; + dub2nix = (import dub2nix-src) { inherit pkgs; }; +in +with pkgs; [ + nixFlakes + dub + ldc + sqlite + nix-prefetch-git + validatePkgConfig +] -- cgit v1.2.3