core/pkgs/build-support/agda/default.nix

149 lines
3.9 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
# Builder for Agda packages.
2024-06-30 08:16:52 +00:00
{
stdenv,
lib,
self,
Agda,
runCommand,
makeWrapper,
writeText,
ghcWithPackages,
nixosTests,
}:
2024-05-02 00:46:19 +00:00
let
inherit (lib)
attrValues
elem
filter
filterAttrs
isAttrs
isList
platforms
;
2024-06-30 08:16:52 +00:00
inherit (lib.strings) concatMapStrings concatMapStringsSep optionalString;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
withPackages' =
{
pkgs,
ghc ? ghcWithPackages (p: with p; [ ieee754 ]),
}:
let
pkgs' = if isList pkgs then pkgs else pkgs self;
library-file = writeText "libraries" ''
${(concatMapStringsSep "\n" (p: "${p}/${p.libraryFile}") pkgs')}
'';
pname = "agdaWithPackages";
version = Agda.version;
in
runCommand "${pname}-${version}"
{
inherit pname version;
nativeBuildInputs = [ makeWrapper ];
passthru = {
unwrapped = Agda;
inherit withPackages;
tests = {
inherit (nixosTests) agda;
allPackages = withPackages (filter self.lib.isUnbrokenAgdaPackage (attrValues self));
};
};
# Agda is a split package with multiple outputs; do not inherit them here.
meta = removeAttrs Agda.meta [ "outputsToInstall" ];
}
''
mkdir -p $out/bin
makeWrapper ${Agda.bin}/bin/agda $out/bin/agda \
--add-flags "--with-compiler=${ghc}/bin/ghc" \
--add-flags "--library-file=${library-file}"
ln -s ${Agda.bin}/bin/agda-mode $out/bin/agda-mode
'';
2024-05-02 00:46:19 +00:00
withPackages = arg: if isAttrs arg then withPackages' arg else withPackages' { pkgs = arg; };
extensions = [
"agda"
"agda-lib"
"agdai"
"lagda"
"lagda.md"
"lagda.org"
"lagda.rst"
"lagda.tex"
"lagda.typ"
];
defaults =
2024-06-30 08:16:52 +00:00
{
pname,
meta,
buildInputs ? [ ],
everythingFile ? "./Everything.agda",
includePaths ? [ ],
libraryName ? pname,
libraryFile ? "${libraryName}.agda-lib",
buildPhase ? null,
installPhase ? null,
extraExtensions ? [ ],
...
}:
let
2024-05-02 00:46:19 +00:00
agdaWithArgs = withPackages (filter (p: p ? isAgdaDerivation) buildInputs);
2024-06-30 08:16:52 +00:00
includePathArgs = concatMapStrings (path: "-i" + path + " ") (
includePaths ++ [ (dirOf everythingFile) ]
);
2024-05-02 00:46:19 +00:00
in
2024-06-30 08:16:52 +00:00
{
inherit libraryName libraryFile;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
isAgdaDerivation = true;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
buildInputs = buildInputs ++ [ agdaWithArgs ];
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
buildPhase =
if buildPhase != null then
buildPhase
else
''
runHook preBuild
agda ${includePathArgs} ${everythingFile}
rm ${everythingFile} ${lib.interfaceFile Agda.version everythingFile}
runHook postBuild
'';
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
installPhase =
if installPhase != null then
installPhase
else
''
runHook preInstall
mkdir -p $out
find \( ${
concatMapStringsSep " -or " (p: "-name '*.${p}'") (extensions ++ extraExtensions)
} \) -exec cp -p --parents -t "$out" {} +
runHook postInstall
'';
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# As documented at https://github.com/NixOS/nixpkgs/issues/172752,
# we need to set LC_ALL to an UTF-8-supporting locale. However, on
# darwin, it seems that there is no standard such locale; luckily,
# the referenced issue doesn't seem to surface on darwin. Hence let's
# set this only on non-darwin.
LC_ALL = optionalString (!stdenv.isDarwin) "C.UTF-8";
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
meta = if meta.broken or false then meta // { hydraPlatforms = platforms.none; } else meta;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Retrieve all packages from the finished package set that have the current package as a dependency and build them
passthru.tests = filterAttrs (
name: pkg: self.lib.isUnbrokenAgdaPackage pkg && elem pname (map (pkg: pkg.pname) pkg.buildInputs)
) self;
};
2024-05-02 00:46:19 +00:00
in
{
mkDerivation = args: stdenv.mkDerivation (args // defaults args);
inherit withPackages withPackages';
}