Skip to content

Commit

Permalink
update buildIdris function to support withSource retroactively (#3469)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin authored Jan 21, 2025
1 parent 82ea4b1 commit 8e420d6
Showing 1 changed file with 116 additions and 61 deletions.
177 changes: 116 additions & 61 deletions nix/buildIdris.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
# in {
# lib = pkg.library { withSource = true; };
# bin = pkg.executable;
#
# # implicitly without source:
# lib' = pkg.library'
#
# # retroactively with source:
# lib'' = pkg.library'.withSource
# }
#
{ src
Expand All @@ -34,79 +40,102 @@ let
ipkgFileName = ipkgName + ".ipkg";
idrName = "idris2-${idris2Version}";
libSuffix = "lib/${idrName}";
propagatedIdrisLibraries = propagate idrisLibraryLibs;
libDirs =
lib.strings.makeSearchPath libSuffix propagatedIdrisLibraries;
libDirs = libs: lib.strings.makeSearchPath libSuffix libs;
drvAttrs = builtins.removeAttrs attrs [
"ipkgName"
"idrisLibraries"
];

derivation = stdenv.mkDerivation (finalAttrs:
drvAttrs // {
pname = ipkgName;
inherit version;
src = src;
nativeBuildInputs = [ idris2 makeWrapper jq ] ++ attrs.nativeBuildInputs or [];
buildInputs = propagatedIdrisLibraries ++ attrs.buildInputs or [];
mkDerivation =
withSource:
let
applyWithSource = lib: if withSource then lib.withSource else lib;
propagatedIdrisLibraries = map applyWithSource (propagate idrisLibraryLibs);
in
stdenv.mkDerivation (
finalAttrs:
drvAttrs
// {
pname = ipkgName;
inherit src version;
nativeBuildInputs = [
idris2
makeWrapper
jq
] ++ attrs.nativeBuildInputs or [ ];
buildInputs = propagatedIdrisLibraries ++ attrs.buildInputs or [ ];

env.IDRIS2_PACKAGE_PATH = libDirs;
env.IDRIS2_PACKAGE_PATH = libDirs propagatedIdrisLibraries;

buildPhase = ''
runHook preBuild
idris2 --build ${ipkgFileName}
runHook postBuild
'';
buildPhase = ''
runHook preBuild
idris2 --build ${ipkgFileName}
runHook postBuild
'';

passthru = {
inherit propagatedIdrisLibraries;
} // (attrs.passthru or {});
passthru = {
inherit propagatedIdrisLibraries;
} // (attrs.passthru or { });

shellHook = ''
export IDRIS2_PACKAGE_PATH="${finalAttrs.env.IDRIS2_PACKAGE_PATH}"
'';
}
);

in rec {
executable = derivation.overrideAttrs {
installPhase = ''
runHook preInstall
mkdir -p $out/bin
scheme_app="$(find ./build/exec -name '*_app')"
if [ "$scheme_app" = ''' ]; then
mv -- build/exec/* $out/bin/
chmod +x $out/bin/*
# ^ remove after Idris2 0.8.0 is released. will be superfluous:
# https://github.com/idris-lang/Idris2/pull/3189
else
bin_name="$(idris2 --dump-ipkg-json ${ipkgFileName} | jq -r '.executable')"
cd build/exec/''${bin_name}_app
rm -f ./libidris2_support.{so,dylib}
executable="''${bin_name}.so"
mv -- "$executable" "$out/bin/$bin_name"
# remaining .so or .dylib files can be moved to lib directory
for file in *{.so,.dylib}; do
mkdir -p $out/lib
mv -- "$file" "$out/lib/"
done
shellHook = ''
export IDRIS2_PACKAGE_PATH="${finalAttrs.env.IDRIS2_PACKAGE_PATH}"
'';
}
);

mkExecutable =
withSource:
let
derivation = mkDerivation withSource;
in
derivation.overrideAttrs {
installPhase = ''
runHook preInstall
mkdir -p $out/bin
scheme_app="$(find ./build/exec -name '*_app')"
if [ "$scheme_app" = ''' ]; then
mv -- build/exec/* $out/bin/
chmod +x $out/bin/*
# ^ remove after Idris2 0.8.0 is released. will be superfluous:
# https://github.com/idris-lang/Idris2/pull/3189
else
bin_name="$(idris2 --dump-ipkg-json ${ipkgFileName} | jq -r '.executable')"
cd build/exec/''${bin_name}_app
rm -f ./libidris2_support.{so,dylib}
executable="''${bin_name}.so"
mv -- "$executable" "$out/bin/$bin_name"
wrapProgram "$out/bin/$bin_name" \
--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ support ]}:$out/lib \
--prefix DYLD_LIBRARY_PATH : ${lib.makeLibraryPath [ support ]}:$out/lib
# remaining .so or .dylib files can be moved to lib directory
for file in *{.so,.dylib}; do
mkdir -p $out/lib
mv -- "$file" "$out/lib/"
done
fi
runHook postInstall
'';
};
wrapProgram "$out/bin/$bin_name" \
--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ support ]}:$out/lib \
--prefix DYLD_LIBRARY_PATH : ${lib.makeLibraryPath [ support ]}:$out/lib
library = { withSource ? false }:
let installCmd = if withSource then "--install-with-src" else "--install";
in derivation.overrideAttrs {
fi
runHook postInstall
'';

# allow an executable's dependencies to be built with source. this is convenient when
# building a development shell for the exectuable using `mkShell`'s `inputsFrom`.
passthru = derivation.passthru // {
withSource = mkExecutable true;
};
};

mkLibrary =
withSource:
let
installCmd = if withSource then "--install-with-src" else "--install";
derivation = mkDerivation withSource;
in
derivation.overrideAttrs {
installPhase = ''
runHook preInstall
mkdir -p $out/${libSuffix}
Expand All @@ -121,7 +150,33 @@ in rec {
done
runHook postInstall
'';

# allow a library built without source to be changed to one with source
# via a passthru attribute; i.e. `(my-pkg.library {}).withSource`.
# this is convenient because a library derivation can be distributed as
# without-source by default but downstream projects can still build it
# with-source. We surface this regardless of whether the original library
# was built with source because that allows downstream to call this
# property unconditionally.
passthru = derivation.passthru // {
withSource = mkLibrary true;
};
};

in
rec {
executable = mkExecutable false;

library =
{
withSource ? false,
}:
mkLibrary withSource;

# Make a library without source; you can still use the `withSource` attribute
# on the resulting derivation to build the library with source at a later time.
library' = mkLibrary false;

# deprecated aliases:
build = lib.warn "build is a deprecated alias for 'executable'." executable;
installLibrary = lib.warn "installLibrary is a deprecated alias for 'library { }'." (library { });
Expand Down

0 comments on commit 8e420d6

Please sign in to comment.