Skip to content
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
165 changes: 44 additions & 121 deletions pkgs/build-support/dlang/builddubpackage/default.nix
Original file line number Diff line number Diff line change
@@ -1,148 +1,71 @@
{
lib,
stdenv,
fetchurl,
fetchgit,
linkFarm,
dub,
importDubLock,
dubSetupHook,
dubBuildHook,
dubCheckHook,
ldc,
removeReferencesTo,
}:

# See https://nixos.org/manual/nixpkgs/unstable#dlang for more detailed usage information
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;

{
# 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:
excludeDrvArgNames = [
"dubLock"
"compiler"
];

let
makeDubDep =
{
pname,
version,
sha256,
}:
extendDrvArgs =
finalAttrs:
{
inherit pname version;
src = fetchurl {
name = "dub-${pname}-${version}.zip";
url = "mirror://dub/${pname}/${version}.zip";
inherit sha256;
};
};
# 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,
compiler ? ldc,
...
}@args:

makeGitDep =
{
pname,
version,
repository,
sha256,
}:
{
inherit pname version;
src = fetchgit {
url = repository;
rev = version;
inherit sha256;
dubDeps = importDubLock {
inherit (finalAttrs) pname version;
lock = dubLock;
};
};

lockJson = if lib.isPath dubLock then lib.importJSON dubLock else dubLock;
depsRaw = lib.mapAttrsToList (pname: args: { inherit pname; } // args) lockJson.dependencies;

dubDeps = map makeDubDep (lib.filter (args: !(args ? repository)) depsRaw);
gitDeps = map makeGitDep (lib.filter (args: args ? repository) depsRaw);

# 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;
}) dubDeps
);
strictDeps = args.strictDeps or true;

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 [ ] ++ [
dubSetupHook
(dubBuildHook.override { inherit dub; })
compiler
removeReferencesTo
];

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

configurePhase =
args.configurePhase or ''
configurePhase = ''
runHook preConfigure

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

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

# register git dependencies
${lib.concatMapStringsSep "\n" (dep: ''
mkdir -p "$DUB_HOME/packages/${dep.pname}/${dep.version}"
cp -r --no-preserve=all ${dep.src} "$DUB_HOME/packages/${dep.pname}/${dep.version}/${dep.pname}"
'') gitDeps}

dubFlags+=("--compiler=${lib.getExe compiler}")
runHook postConfigure
'';

buildPhase =
args.buildPhase or ''
runHook preBuild

dub build ${combinedBuildFlags}

runHook postBuild
'';

doCheck = args.doCheck or false;
doCheck = args.doCheck or false;

checkPhase =
args.checkPhase or ''
runHook preCheck
nativeCheckInputs = [
(dubCheckHook.override { inherit dub; })
];

dub test ${combinedTestFlags}
preFixup = ''
${args.preFixup or ""}

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

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

find "$out" -type f -exec remove-references-to -t ${compiler} '{}' +
'';
disallowedReferences = args.disallowedReferences or [ compiler ];

disallowedReferences = [ compiler ];

meta = {
platforms = dub.meta.platforms;
}
// args.meta or { };
}
)
meta = {
platforms = dub.meta.platforms;
}
// args.meta or { };
};
}
26 changes: 26 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/hooks/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ callPackage }:

{
dubSetupHook = callPackage (
{ makeSetupHook }:
makeSetupHook {
name = "dub-setup-hook";
} ./dub-setup-hook.sh
) { };

dubBuildHook = callPackage (
{ makeSetupHook, dub }:
makeSetupHook {
name = "dub-build-hook";
propagatedBuildInputs = [ dub ];
} ./dub-build-hook.sh
) { };

dubCheckHook = callPackage (
{ makeSetupHook, dub }:
makeSetupHook {
name = "dub-check-hook";
propagatedBuildInputs = [ dub ];
} ./dub-check-hook.sh
) { };
}
13 changes: 13 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/hooks/dub-build-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dubBuildHook() {
runHook preBuild
echo "Executing dubBuildHook"

dub build --skip-registry=all --build="${dubBuildType-"release"}" "${dubBuildFlags[@]}" "${dubFlags[@]}"

echo "Finished dubBuildHook"
runHook postBuild
}

if [[ -z "${dontDubBuild-}" && -z "${buildPhase-}" ]]; then
buildPhase=dubBuildHook
fi
13 changes: 13 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/hooks/dub-check-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dubCheckHook() {
runHook preCheck
echo "Executing dubCheckHook"

dub test --skip-registry=all "${dubTestFlags[@]}" "${dubFlags[@]}"

echo "Finished dubCheckHook"
runHook postCheck
}

if [[ -z "${dontDubCheck-}" && -z "${checkPhase-}" ]]; then
checkPhase=dubCheckHook
fi
19 changes: 19 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/hooks/dub-setup-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dubSetupHook() {
echo "Executing dubSetupHook"

if [ -z "${dubDeps-}" ]; then
echo "Error: 'dubDeps' must be set when using dubSetupHook."
exit 1
fi

export DUB_HOME="$NIX_BUILD_TOP"/.dub
echo "Configuring \$DUB_HOME ($DUB_HOME)"
cp -Tr "$dubDeps"/.dub "$NIX_BUILD_TOP"/.dub
chmod -R +w "$DUB_HOME"

echo "Finished dubSetupHook"
}

if [ -z "${dontDubSetup-}" ]; then
postPatchHooks+=(dubSetupHook)
fi
81 changes: 81 additions & 0 deletions pkgs/build-support/dlang/builddubpackage/import-dub-lock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
lib,
runCommand,
linkFarm,
fetchurl,
fetchgit,
dub,
}:

{
pname,
version,
lock,
}:

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

makeGitDep =
{
pname,
version,
repository,
sha256,
}:
{
inherit pname version;
src = fetchgit {
url = repository;
rev = version;
inherit sha256;
};
};

lockJson = if lib.isPath lock then lib.importJSON lock else lock;
depsRaw = lib.mapAttrsToList (pname: args: { inherit pname; } // args) lockJson.dependencies;

dubDeps = map makeDubDep (lib.filter (args: !(args ? repository)) depsRaw);
gitDeps = map makeGitDep (lib.filter (args: args ? repository) depsRaw);

# 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;
}) dubDeps
);

in
runCommand "${pname}-${version}-dub-deps"
{
nativeBuildInputs = [ dub ];
}
''
export DUB_HOME="$out/.dub"
mkdir -p "$DUB_HOME"
# register dub dependencies
${lib.concatMapStringsSep "\n" (dep: ''
dub fetch ${dep.pname}@${dep.version} --cache=user --skip-registry=standard --registry=file://${dubRegistryBase}/${dep.pname}
'') dubDeps}
# register git dependencies
${lib.concatMapStringsSep "\n" (dep: ''
mkdir -p "$DUB_HOME/packages/${dep.pname}/${dep.version}"
cp -r --no-preserve=all ${dep.src} "$DUB_HOME/packages/${dep.pname}/${dep.version}/${dep.pname}"
'') gitDeps}
''
5 changes: 4 additions & 1 deletion pkgs/build-support/dlang/dub-support.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{ callPackage }:

{
buildDubPackage = callPackage ./builddubpackage { };
dub-to-nix = callPackage ./dub-to-nix { };
importDubLock = callPackage ./builddubpackage/import-dub-lock.nix { };
buildDubPackage = callPackage ./builddubpackage { };
}
// import ./builddubpackage/hooks { inherit callPackage; }
Empty file modified pkgs/build-support/dlang/dub-to-nix/dub-to-nix.py
100644 → 100755
Empty file.
6 changes: 5 additions & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2803,8 +2803,12 @@ with pkgs;
dsview = libsForQt5.callPackage ../applications/science/electronics/dsview { };

inherit (import ../build-support/dlang/dub-support.nix { inherit callPackage; })
buildDubPackage
dub-to-nix
importDubLock
buildDubPackage
dubSetupHook
dubBuildHook
dubCheckHook
;

duff = callPackage ../tools/filesystems/duff { };
Expand Down
Loading