Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildDubPackage and dub-to-nix for building dub based packages #299618

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions doc/languages-frameworks/dlang.section.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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;
TomaSajt marked this conversation as resolved.
Show resolved Hide resolved

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 change: 1 addition & 0 deletions doc/languages-frameworks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions nixos/doc/manual/release-notes/rl-2405.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,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.
Expand Down
7 changes: 7 additions & 0 deletions pkgs/build-support/dlang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Build support for D

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

### Current maintainers
- @TomaSajt
- @jtbx
124 changes: 124 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
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 { };
}
)
5 changes: 5 additions & 0 deletions pkgs/build-support/dlang/dub-support.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ callPackage }:
{
buildDubPackage = callPackage ./builddubpackage { };
dub-to-nix = callPackage ./dub-to-nix { };
}
19 changes: 19 additions & 0 deletions pkgs/build-support/dlang/dub-to-nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
lib,
runCommand,
makeWrapper,
python3,
nix,
}:

runCommand "dub-to-nix"
{
nativeBuildInputs = [ makeWrapper ];
buildInputs = [ python3 ];
}
''
install -Dm755 ${./dub-to-nix.py} "$out/bin/dub-to-nix"
patchShebangs "$out/bin/dub-to-nix"
wrapProgram "$out/bin/dub-to-nix" \
--prefix PATH : ${lib.makeBinPath [ nix ]}
''
39 changes: 39 additions & 0 deletions pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3

import sys
import json
import os
import subprocess

def eprint(text: str):
print(text, file=sys.stderr)

if not os.path.exists("dub.selections.json"):
eprint("The file `dub.selections.json` does not exist in the current working directory")
eprint("run `dub upgrade --annotate` to generate it")
sys.exit(1)

with open("dub.selections.json") as f:
selectionsJson = json.load(f)

versionDict: dict[str, str] = selectionsJson["versions"]

for pname in versionDict:
version = versionDict[pname]
if version.startswith("~"):
eprint(f'Package "{pname}" has a branch-type version "{version}", which doesn\'t point to a fixed version')
eprint("You can resolve it by manually changing the required version to a fixed one inside `dub.selections.json`")
eprint("When packaging, you might need to create a patch for `dub.sdl` or `dub.json` to accept the changed version")
sys.exit(1)

lockedDependenciesDict: dict[str, dict[str, str]] = {}

for pname in versionDict:
version = versionDict[pname]
eprint(f"Fetching {pname}@{version}")
url = f"https://code.dlang.org/packages/{pname}/{version}.zip"
command = ["nix-prefetch-url", "--type", "sha256", url]
sha256 = subprocess.run(command, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.rstrip()
lockedDependenciesDict[pname] = {"version": version, "sha256": sha256}

print(json.dumps({"dependencies": lockedDependenciesDict}, indent=2))
5 changes: 5 additions & 0 deletions pkgs/build-support/fetchurl/mirrors.nix
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@
"https://backpan.perl.org/" # for old releases
];

# D DUB
dub = [
"https://code.dlang.org/packages/"
];

# Haskell Hackage
hackage = [
"https://hackage.haskell.org/package/"
Expand Down
Loading