Unverified Commit b136700c authored by Atemu's avatar Atemu Committed by GitHub
Browse files

Merge pull request #299618 from TomaSajt/dub-support

Add buildDubPackage and dub-to-nix for building dub based packages
parents d5da6daf b7efb314
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
# D (Dlang) {#dlang}

Nixpkgs provides multiple D compilers such as `ldc`, `dmd` and `gdc`.
These can be used like any other package during build time.

However, Nixpkgs provides a build helper for compiling packages using the `dub` package manager.

Here's an example:
```nix
{
  lib,
  buildDubPackage,
  fetchFromGitHub,
  ncurses,
  zlib,
}:

buildDubPackage rec {
  pname = "btdu";
  version = "0.5.1";

  src = fetchFromGitHub {
    owner = "CyberShadow";
    repo = "btdu";
    rev = "v${version}";
    hash = "sha256-3sSZq+5UJH02IO0Y1yL3BLHDb4lk8k6awb5ZysBQciE=";
  };

  # generated by dub-to-nix, see below
  dubLock = ./dub-lock.json;

  buildInputs = [
    ncurses
    zlib
  ];

  installPhase = ''
    runHook preInstall
    install -Dm755 btdu -t $out/bin
    runHook postInstall
  '';
}
```

Note that you need to define `installPhase` because `dub` doesn't know where files should go in `$out`.

Also note that running `dub test` is disabled by default. You can enable it by setting `doCheck = true`.

## Lockfiles {#dub-lockfiles}
Nixpkgs has its own lockfile format for `dub` dependencies, because `dub`'s official "lockfile" format (`dub.selections.json`) is not hash based.

A lockfile can be generated using the `dub-to-nix` helper package.
* Firstly, install `dub-to-nix` into your shell session by running `nix-shell -p dub-to-nix`
* Then navigate to the root of the source of the program you want to package
* Finally, run `dub-to-nix` and it will print the lockfile to stdout. You could pipe stdout into a text file or just copy the output manually into a file.

## `buildDubPackage` parameters {#builddubpackage-parameters}

The `buildDubPackage` function takes an attrset of parameters that are passed on to `stdenv.mkDerivation`.

The following parameters are specific to `buildDubPackage`:

* `dubLock`: A lockfile generated by `dub-to-nix` from the source of the package. Can be either a path to the file, or an attrset already parsed with `lib.importJSON`.
  The latter useful if the package uses `dub` dependencies not already in the lockfile. (e.g. if the package calls `dub run some-dub-package` manually)
* `dubBuildType ? "release"`: The build type to pass to `dub build` as a value for the `--build=` flag.
* `dubFlags ? []`: The flags to pass to `dub build` and `dub test`.
* `dubBuildFlags ? []`: The flags to pass to `dub build`.
* `dubTestFlags ? []`: The flags to pass to `dub test`.
* `compiler ? ldc`: The D compiler to be used by `dub`.
+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ cuda.section.md
cuelang.section.md
dart.section.md
dhall.section.md
dlang.section.md
dotnet.section.md
emscripten.section.md
gnome.section.md
+3 −0
Original line number Diff line number Diff line
@@ -402,6 +402,9 @@ The pre-existing [services.ankisyncd](#opt-services.ankisyncd.enable) has been m
  The `nimPackages` and `nim2Packages` sets have been removed.
  See https://nixos.org/manual/nixpkgs/unstable#nim for more information.

- Programs written in [D](https://dlang.org/) using the `dub` build system and package manager can now be built using `buildDubPackage` utilizing lockfiles provided by the new `dub-to-nix` helper program.
  See the [D section](https://nixos.org/manual/nixpkgs/unstable#dlang) in the manual for more information.

- [Portunus](https://github.com/majewsky/portunus) has been updated to major version 2.
  This version of Portunus supports strong password hashes, but the legacy hash SHA-256 is also still supported to ensure a smooth migration of existing user accounts.
  After upgrading, follow the instructions on the [upstream release notes](https://github.com/majewsky/portunus/releases/tag/v2.0.0) to upgrade all user accounts to strong password hashes.
+7 −0
Original line number Diff line number Diff line
# Build support for D

Build utilities for the D language can be found in this directory.

### Current maintainers
- @TomaSajt
- @jtbx
+124 −0
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  fetchurl,
  linkFarm,
  dub,
  ldc,
  removeReferencesTo,
}:

# See https://nixos.org/manual/nixpkgs/unstable#dlang for more detailed usage information

{
  # A lockfile generated by `dub-to-nix` from the source of the package.
  # Can be either a path to the file, or an attrset already parsed with `lib.importJSON`.
  dubLock,
  # The build type to pass to `dub build` as a value for the `--build=` flag.
  dubBuildType ? "release",
  # The flags to pass to `dub build` and `dub test`.
  dubFlags ? [ ],
  # The flags to pass to `dub build`.
  dubBuildFlags ? [ ],
  # The flags to pass to `dub test`.
  dubTestFlags ? [ ],
  # The D compiler to be used by `dub`.
  compiler ? ldc,
  ...
}@args:

let
  makeDubDep =
    {
      pname,
      version,
      sha256,
    }:
    {
      inherit pname version;
      src = fetchurl {
        name = "dub-${pname}-${version}.zip";
        url = "mirror://dub/${pname}/${version}.zip";
        inherit sha256;
      };
    };

  lockJson = if lib.isPath dubLock then lib.importJSON dubLock else dubLock;

  lockedDeps = lib.mapAttrsToList (
    pname: { version, sha256 }: makeDubDep { inherit pname version sha256; }
  ) lockJson.dependencies;

  # a directory with multiple single element registries
  # one big directory with all .zip files leads to version parsing errors
  # when the name of a package is a prefix of the name of another package
  dubRegistryBase = linkFarm "dub-registry-base" (
    map (dep: {
      name = "${dep.pname}/${dep.pname}-${dep.version}.zip";
      path = dep.src;
    }) lockedDeps
  );

  combinedFlags = "--skip-registry=all --compiler=${lib.getExe compiler} ${toString dubFlags}";
  combinedBuildFlags = "${combinedFlags} --build=${dubBuildType} ${toString dubBuildFlags}";
  combinedTestFlags = "${combinedFlags} ${toString dubTestFlags}";
in
stdenv.mkDerivation (
  builtins.removeAttrs args [ "dubLock" ]
  // {
    strictDeps = args.strictDeps or true;

    nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
      dub
      compiler
      removeReferencesTo
    ];

    configurePhase =
      args.configurePhase or ''
        runHook preConfigure

        export DUB_HOME="$NIX_BUILD_TOP/.dub"
        mkdir -p $DUB_HOME

        # register dependencies
        ${lib.concatMapStringsSep "\n" (dep: ''
          dub fetch ${dep.pname}@${dep.version} --cache=user --skip-registry=standard --registry=file://${dubRegistryBase}/${dep.pname}
        '') lockedDeps}

        runHook postConfigure
      '';

    buildPhase =
      args.buildPhase or ''
        runHook preBuild

        dub build ${combinedBuildFlags}

        runHook postBuild
      '';

    doCheck = args.doCheck or false;

    checkPhase =
      args.checkPhase or ''
        runHook preCheck

        dub test ${combinedTestFlags}

        runHook postCheck
      '';

    preFixup = ''
      ${args.preFixup or ""}

      find "$out" -type f -exec remove-references-to -t ${compiler} '{}' +
    '';

    disallowedReferences = [ compiler ];

    meta = {
      platforms = dub.meta.platforms;
    } // args.meta or { };
  }
)
Loading