Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# Builder for Agda packages.
2
3{
4 stdenv,
5 lib,
6 self,
7 Agda,
8 runCommand,
9 makeWrapper,
10 writeText,
11 ghcWithPackages,
12 nixosTests,
13}:
14
15let
16 inherit (lib)
17 attrValues
18 elem
19 filter
20 filterAttrs
21 isAttrs
22 isList
23 platforms
24 ;
25
26 inherit (lib.strings)
27 concatMapStrings
28 concatMapStringsSep
29 optionalString
30 ;
31
32 mkLibraryFile =
33 pkgs:
34 let
35 pkgs' = if isList pkgs then pkgs else pkgs self;
36 in
37 writeText "libraries" ''
38 ${(concatMapStringsSep "\n" (p: "${p}/${p.libraryFile}") pkgs')}
39 '';
40
41 withPackages' =
42 {
43 pkgs,
44 ghc ? ghcWithPackages (p: with p; [ ieee754 ]),
45 }:
46 let
47 library-file = mkLibraryFile pkgs;
48 pname = "agdaWithPackages";
49 version = Agda.version;
50 in
51 runCommand "${pname}-${version}"
52 {
53 inherit pname version;
54 nativeBuildInputs = [ makeWrapper ];
55 passthru = {
56 unwrapped = Agda;
57 inherit withPackages;
58 tests = {
59 inherit (nixosTests) agda;
60 allPackages = withPackages (filter self.lib.isUnbrokenAgdaPackage (attrValues self));
61 };
62 };
63 # Agda is a split package with multiple outputs; do not inherit them here.
64 meta = removeAttrs Agda.meta [ "outputsToInstall" ];
65 }
66 ''
67 mkdir -p $out/bin
68 makeWrapper ${lib.getExe Agda} $out/bin/agda \
69 ${lib.optionalString (ghc != null) ''--add-flags "--with-compiler=${ghc}/bin/ghc"''} \
70 --add-flags "--library-file=${library-file}"
71 ln -s ${lib.getExe' Agda "agda-mode"} $out/bin/agda-mode
72 '';
73
74 withPackages = arg: if isAttrs arg then withPackages' arg else withPackages' { pkgs = arg; };
75
76 extensions = [
77 "agda"
78 "agda-lib"
79 "agdai"
80 "lagda"
81 "lagda.md"
82 "lagda.org"
83 "lagda.rst"
84 "lagda.tex"
85 "lagda.typ"
86 ];
87
88 defaults =
89 {
90 pname,
91 meta,
92 buildInputs ? [ ],
93 libraryName ? pname,
94 libraryFile ? "${libraryName}.agda-lib",
95 buildPhase ? null,
96 installPhase ? null,
97 extraExtensions ? [ ],
98 ...
99 }:
100 let
101 agdaWithPkgs = withPackages (filter (p: p ? isAgdaDerivation) buildInputs);
102 in
103 {
104 inherit libraryName libraryFile;
105
106 isAgdaDerivation = true;
107
108 buildInputs = buildInputs ++ [ agdaWithPkgs ];
109
110 buildPhase =
111 if buildPhase != null then
112 buildPhase
113 else
114 ''
115 runHook preBuild
116 agda --build-library
117 runHook postBuild
118 '';
119
120 installPhase =
121 if installPhase != null then
122 installPhase
123 else
124 ''
125 runHook preInstall
126 mkdir -p $out
127 find \( ${
128 concatMapStringsSep " -or " (p: "-name '*.${p}'") (extensions ++ extraExtensions)
129 } \) -exec cp -p --parents -t "$out" {} +
130 runHook postInstall
131 '';
132
133 # As documented at https://github.com/NixOS/nixpkgs/issues/172752,
134 # we need to set LC_ALL to an UTF-8-supporting locale. However, on
135 # darwin, it seems that there is no standard such locale; luckily,
136 # the referenced issue doesn't seem to surface on darwin. Hence let's
137 # set this only on non-darwin.
138 LC_ALL = optionalString (!stdenv.hostPlatform.isDarwin) "C.UTF-8";
139
140 meta = if meta.broken or false then meta // { hydraPlatforms = platforms.none; } else meta;
141
142 # Retrieve all packages from the finished package set that have the current package as a dependency and build them
143 passthru.tests = filterAttrs (
144 name: pkg: self.lib.isUnbrokenAgdaPackage pkg && elem pname (map (pkg: pkg.pname) pkg.buildInputs)
145 ) self;
146 };
147in
148{
149 mkDerivation = args: stdenv.mkDerivation (args // defaults args);
150
151 inherit mkLibraryFile withPackages withPackages';
152}