nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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 everythingFile ? "./Everything.agda",
94 includePaths ? [ ],
95 libraryName ? pname,
96 libraryFile ? "${libraryName}.agda-lib",
97 buildPhase ? null,
98 installPhase ? null,
99 extraExtensions ? [ ],
100 ...
101 }:
102 let
103 agdaWithArgs = withPackages (filter (p: p ? isAgdaDerivation) buildInputs);
104 includePathArgs = concatMapStrings (path: "-i" + path + " ") (
105 includePaths ++ [ (dirOf everythingFile) ]
106 );
107 in
108 {
109 inherit libraryName libraryFile;
110
111 isAgdaDerivation = true;
112
113 buildInputs = buildInputs ++ [ agdaWithArgs ];
114
115 buildPhase =
116 if buildPhase != null then
117 buildPhase
118 else
119 ''
120 runHook preBuild
121 agda ${includePathArgs} ${everythingFile}
122 rm ${everythingFile} ${lib.interfaceFile Agda.version everythingFile}
123 runHook postBuild
124 '';
125
126 installPhase =
127 if installPhase != null then
128 installPhase
129 else
130 ''
131 runHook preInstall
132 mkdir -p $out
133 find \( ${
134 concatMapStringsSep " -or " (p: "-name '*.${p}'") (extensions ++ extraExtensions)
135 } \) -exec cp -p --parents -t "$out" {} +
136 runHook postInstall
137 '';
138
139 # As documented at https://github.com/NixOS/nixpkgs/issues/172752,
140 # we need to set LC_ALL to an UTF-8-supporting locale. However, on
141 # darwin, it seems that there is no standard such locale; luckily,
142 # the referenced issue doesn't seem to surface on darwin. Hence let's
143 # set this only on non-darwin.
144 LC_ALL = optionalString (!stdenv.hostPlatform.isDarwin) "C.UTF-8";
145
146 meta = if meta.broken or false then meta // { hydraPlatforms = platforms.none; } else meta;
147
148 # Retrieve all packages from the finished package set that have the current package as a dependency and build them
149 passthru.tests = filterAttrs (
150 name: pkg: self.lib.isUnbrokenAgdaPackage pkg && elem pname (map (pkg: pkg.pname) pkg.buildInputs)
151 ) self;
152 };
153in
154{
155 mkDerivation = args: stdenv.mkDerivation (args // defaults args);
156
157 inherit mkLibraryFile withPackages withPackages';
158}