Skip to content

Commit b81e5f9

Browse files
committed
feat: Persist the package metadata
1 parent f75e9ad commit b81e5f9

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

pkgs/emacs/data/default.nix

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ in
2525
inventories,
2626
flakeLockFile,
2727
archiveLockFile,
28+
metadataJsonFile,
2829
inputOverrides,
2930
elispPackagePins,
3031
defaultMainIsAscii,
32+
persistMetadata,
3133
}: mode: let
3234
toLockData = {
3335
nodes,
@@ -48,6 +50,11 @@ in
4850
then lib.importJSON archiveLockFile
4951
else {};
5052

53+
cachedMetadata =
54+
if mode == "build" && persistMetadata && pathExists metadataJsonFile
55+
then lib.importJSON metadataJsonFile
56+
else {};
57+
5158
makeInventory = import ./inventory {
5259
inherit lib flakeLockData archiveLockData;
5360
};
@@ -92,8 +99,11 @@ in
9299
or (throw "Inventory named ${pin} does not exist")
93100
);
94101

102+
getPackageData0 =
103+
import ./package.nix {inherit lib linkFarm defaultMainIsAscii cachedMetadata;};
104+
95105
getPackageData = revDep: ename:
96-
lib.makeExtensible (import ./package.nix {inherit lib linkFarm defaultMainIsAscii;}
106+
lib.makeExtensible (getPackageData0
97107
ename
98108
# It would be nice if it were possible to set the pin from inside
99109
# overrideInputs, but it causes infinite recursion unfortunately :(

pkgs/emacs/data/package.nix

+20-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ in
2626
lib,
2727
linkFarm,
2828
defaultMainIsAscii,
29+
cachedMetadata,
2930
}: ename: mkAttrs: self: let
3031
attrs =
3132
if isAttrs mkAttrs
@@ -58,6 +59,11 @@ in
5859
)
5960
else {};
6061

62+
metadata =
63+
if hasAttr ename cachedMetadata && cachedMetadata.${ename}.narHash == (self.src.narHash or null)
64+
then cachedMetadata.${ename}
65+
else {};
66+
6167
# builtins.readFile fails when the source file contains control characters.
6268
# pydoc.el is an example. A workaround is to take only the first N bytes of
6369
# the file using `head` command and read its output. This is IFD, which
@@ -136,26 +142,30 @@ in
136142

137143
# TODO: Check https://github.com/melpa/melpa/issues/2955 on the right versioning scheme
138144
version =
139-
attrs.version
145+
metadata.version
146+
or attrs.version
140147
or headers.Version
141148
or headers.Package-Version
142149
or packageDesc.version
143150
# Set a null version if the package lacks a version header.
144151
or null;
145152

146-
author = headers.Author or null;
153+
author =
154+
metadata.author
155+
or headers.Author or null;
147156

148157
meta =
149-
(lib.optionalAttrs hasPkgFile {
150-
description = packageDesc.summary;
151-
})
152-
// (import ./headers-to-meta.nix {
153-
inherit lib;
154-
inherit (self) headers;
155-
});
158+
metadata.meta
159+
or ((lib.optionalAttrs hasPkgFile {
160+
description = packageDesc.summary;
161+
})
162+
// (import ./headers-to-meta.nix {
163+
inherit lib headers;
164+
}));
156165

157166
packageRequires =
158-
attrs.packageRequires
167+
metadata.packageRequires
168+
or attrs.packageRequires
159169
or packageDesc.packageRequires
160170
or (
161171
if headers ? Package-Requires

pkgs/emacs/default.nix

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
if wantExtraOutputs
3737
then ["info"]
3838
else [],
39+
# Whether to persist package metadata in the lock directory. This is needed to
40+
# avoid IFD in certain situations.
41+
persistMetadata ? false,
3942
# Assume the main files of all packages contain only ASCII characters. This is
4043
# a requirement for avoiding IFD, but some libraries actually contain
4144
# non-ASCII characters, which cannot be parsed with `builtins.readFile`
@@ -70,6 +73,8 @@ in
7073

7174
archiveLockFile = lockDir + "/archive.lock";
7275

76+
metadataJsonFile = lockDir + "/metadata.json";
77+
7378
userConfig = lib.pipe self.initFiles [
7479
(map initReader)
7580
lib.zipAttrs
@@ -104,9 +109,11 @@ in
104109
lib
105110
flakeLockFile
106111
archiveLockFile
112+
metadataJsonFile
107113
builtinLibraries
108114
inputOverrides
109115
defaultMainIsAscii
116+
persistMetadata
110117
;
111118
# Just remap the name
112119
inventories = registries;
@@ -234,6 +241,7 @@ in
234241
excludeLocalPackages (enumerateConcretePackageSet "update" explicitPackages);
235242
flakeNix = true;
236243
archiveLock = true;
244+
metadataJson = persistMetadata;
237245
})
238246
.writerScript {inherit postCommandOnGeneratingLockDir;};
239247

@@ -248,6 +256,7 @@ in
248256
excludeLocalPackages (enumerateConcretePackageSet "lock" explicitPackages);
249257
flakeNix = true;
250258
archiveLock = true;
259+
metadataJson = persistMetadata;
251260
postCommand = "nix flake lock";
252261
})
253262
.asAppWritingToRelativeDir
@@ -260,6 +269,7 @@ in
260269
packageInputs =
261270
excludeLocalPackages (enumerateConcretePackageSet "update" explicitPackages);
262271
archiveLock = true;
272+
metadataJson = persistMetadata;
263273
})
264274
.asAppWritingToRelativeDir
265275
lockDirName;

pkgs/emacs/lock/default.nix

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
packageInputs,
1414
flakeNix ? false,
1515
archiveLock ? false,
16+
metadataJson ? false,
1617
# Command run after writing the directory in asAppWritingToRelativeDir
1718
postCommand ? null,
1819
}:
@@ -30,6 +31,20 @@ assert (flakeNix || archiveLock); let
3031
]))
3132
];
3233

34+
packageMetadata =
35+
mapAttrs (_: {
36+
src,
37+
version,
38+
packageRequires,
39+
meta,
40+
author,
41+
...
42+
}: {
43+
inherit (src) narHash;
44+
inherit version packageRequires meta author;
45+
})
46+
packageInputs;
47+
3348
data = {
3449
flakeNix = lib.toNix {
3550
description = "THIS IS AN AUTO-GENERATED FILE. PLEASE DON'T EDIT IT MANUALLY.";
@@ -40,11 +55,13 @@ assert (flakeNix || archiveLock); let
4055
outputs = {...}: {};
4156
};
4257
archiveLock = toJSON archiveLockData;
58+
metadataJson = toJSON packageMetadata;
4359
};
4460

4561
passAsFile =
4662
lib.optional flakeNix "flakeNix"
47-
++ lib.optional archiveLock "archiveLock";
63+
++ lib.optional archiveLock "archiveLock"
64+
++ lib.optional metadataJson "metadataJson";
4865

4966
# HACK: Use sed to convert JSON to Nix
5067
#
@@ -60,6 +77,10 @@ assert (flakeNix || archiveLock); let
6077
${jq}/bin/jq . "$archiveLockPath" > "$out/archive.lock"
6178
'';
6279

80+
generateMetadataJson = ''
81+
${jq}/bin/jq . "$metadataJsonPath" > "$out/metadata.json"
82+
'';
83+
6384
src =
6485
runCommandLocal "emacs-twist-lock" ({
6586
inherit passAsFile;
@@ -70,6 +91,7 @@ assert (flakeNix || archiveLock); let
7091
7192
${lib.optionalString flakeNix generateFlakeNix}
7293
${lib.optionalString archiveLock generateArchiveLock}
94+
${lib.optionalString metadataJson generateMetadataJson}
7395
'';
7496
in {
7597
asAppWritingToRelativeDir = outDir: {

0 commit comments

Comments
 (0)