buildNimPackage: refactor to use overlay-style overrideAttrs

Make buildNimPackage cleaner and more efficient. Also encourage
the use of a "buildNimPackage (finalAttrs: {…})" pattern.
This commit is contained in:
Emery Hemingway 2023-06-14 10:43:26 +01:00
parent 731c00a128
commit 077d8a3447
4 changed files with 64 additions and 73 deletions

View file

@ -15,32 +15,23 @@ case of packages not containing exported library code the attribute
The following example shows a Nim program that depends only on Nim libraries:
```nix
{ lib, nimPackages, fetchurl }:
nimPackages.buildNimPackage rec {
pname = "hottext";
version = "1.4";
{ lib, nimPackages, fetchFromGitHub }:
nimPackages.buildNimPackage (finalAttrs: {
pname = "ttop";
version = "1.0.1";
nimBinOnly = true;
src = fetchurl {
url = "https://git.sr.ht/~ehmry/hottext/archive/v${version}.tar.gz";
hash = "sha256-hIUofi81zowSMbt1lUsxCnVzfJGN3FEiTtN8CEFpwzY=";
src = fetchFromGitHub {
owner = "inv2004";
repo = "ttop";
rev = "v${finalAttrs.version}";
hash = "sha256-x4Uczksh6p3XX/IMrOFtBxIleVHdAPX9e8n32VAUTC4=";
};
buildInputs = with nimPackages; [
bumpy
chroma
flatty
nimsimd
pixie
sdl2
typography
vmath
zippy
];
}
buildInputs = with nimPackages; [ asciigraph illwill parsetoml zippy ];
})
```
## Nim library packages in Nixpkgs {#nim-library-packages-in-nixpkgs}
@ -60,15 +51,16 @@ non-Nim package:
```nix
{ lib, buildNimPackage, fetchNimble, SDL2 }:
buildNimPackage rec {
buildNimPackage (finalAttrs: {
pname = "sdl2";
version = "2.0.4";
src = fetchNimble {
inherit pname version;
hash = "sha256-qDtVSnf+7rTq36WAxgsUZ8XoUk4sKwHyt8EJcY5WP+o=";
inherit (finalAttrs) pname version;
hash = "sha256-Vtcj8goI4zZPQs2TbFoBFlcR5UqDtOldaXSH/+/xULk=";
};
propagatedBuildInputs = [ SDL2 ];
}
doCheck = true;
})
```
## `buildNimPackage` parameters {#buildnimpackage-parameters}

View file

@ -1,45 +1,44 @@
{ lib, stdenv, nim, nim_builder }:
pkgArgs:
{ strictDeps ? true, depsBuildBuild ? [ ], nativeBuildInputs ? [ ]
, configurePhase ? null, buildPhase ? null, checkPhase ? null
, installPhase ? null, enableParallelBuilding ? true, meta ? { }, ... }@attrs:
stdenv.mkDerivation (attrs // {
inherit strictDeps enableParallelBuilding;
depsBuildBuild = [ nim_builder ] ++ depsBuildBuild;
nativeBuildInputs = [ nim ] ++ nativeBuildInputs;
configurePhase = if (configurePhase == null) then ''
runHook preConfigure
export NIX_NIM_BUILD_INPUTS=''${pkgsHostTarget[@]} $NIX_NIM_BUILD_INPUTS
nim_builder --phase:configure
runHook postConfigure
'' else
configurePhase;
buildPhase = if (buildPhase == null) then ''
runHook preBuild
nim_builder --phase:build
runHook postBuild
'' else
buildPhase;
checkPhase = if (checkPhase == null) then ''
runHook preCheck
nim_builder --phase:check
runHook postCheck
'' else
checkPhase;
installPhase = if (installPhase == null) then ''
runHook preInstall
nim_builder --phase:install
runHook postInstall
'' else
installPhase;
meta = meta // {
platforms = meta.platforms or nim.meta.platforms;
maintainers = (meta.maintainers or [ ]) ++ [ lib.maintainers.ehmry ];
let
baseAttrs = {
strictDeps = true;
enableParallelBuilding = true;
configurePhase = ''
runHook preConfigure
export NIX_NIM_BUILD_INPUTS=''${pkgsHostTarget[@]} $NIX_NIM_BUILD_INPUTS
nim_builder --phase:configure
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
nim_builder --phase:build
runHook postBuild
'';
checkPhase = ''
runHook preCheck
nim_builder --phase:check
runHook postCheck
'';
installPhase = ''
runHook preInstall
nim_builder --phase:install
runHook postInstall
'';
meta = { inherit (nim.meta) maintainers platforms; };
};
})
inputsOverride =
{ depsBuildBuild ? [ ], nativeBuildInputs ? [ ], meta, ... }: {
depsBuildBuild = [ nim_builder ] ++ depsBuildBuild;
nativeBuildInputs = [ nim ] ++ nativeBuildInputs;
};
composition = finalAttrs:
let
asFunc = x: if builtins.isFunction x then x else (_: x);
prev = baseAttrs // (asFunc ((asFunc pkgArgs) finalAttrs)) baseAttrs;
in prev // inputsOverride prev;
in stdenv.mkDerivation composition

View file

@ -1,10 +1,10 @@
{ lib, buildNimPackage, fetchNimble, SDL2 }:
buildNimPackage rec {
buildNimPackage (finalAttrs: {
pname = "sdl2";
version = "2.0.4";
src = fetchNimble {
inherit pname version;
inherit (finalAttrs) pname version;
hash = "sha256-Vtcj8goI4zZPQs2TbFoBFlcR5UqDtOldaXSH/+/xULk=";
};
propagatedBuildInputs = [ SDL2 ];
@ -14,4 +14,4 @@ buildNimPackage rec {
platforms = lib.platforms.linux; # Problems with Darwin.
license = [ lib.licenses.mit ];
};
}
})

View file

@ -1,6 +1,6 @@
{ lib, nimPackages, fetchFromGitHub }:
nimPackages.buildNimPackage rec {
nimPackages.buildNimPackage (finalAttrs: {
pname = "ttop";
version = "1.0.1";
nimBinOnly = true;
@ -8,17 +8,17 @@ nimPackages.buildNimPackage rec {
src = fetchFromGitHub {
owner = "inv2004";
repo = "ttop";
rev = "v${version}";
rev = "v${finalAttrs.version}";
hash = "sha256-x4Uczksh6p3XX/IMrOFtBxIleVHdAPX9e8n32VAUTC4=";
};
buildInputs = with nimPackages; [ asciigraph illwill parsetoml zippy ];
meta = with lib;
src.meta // {
finalAttrs.src.meta // {
description = "Top-like system monitoring tool";
license = licenses.mit;
platforms = platforms.linux;
maintainers = with maintainers; [ sikmir ];
};
}
})