Skip to content

Commit 97452cc

Browse files
grahamcEricson2314
authored andcommittedSep 19, 2017
Convert libs to a fixed-point
This does break the API of being able to import any lib file and get its libs, however I'm not sure people did this. I made this while exploring being able to swap out docFn with a stub in #2305, to avoid functor performance problems. I don't know if that is going to move forward (or if it is a problem or not,) but after doing all this work figured I'd put it up anyway :) Two notable advantages to this approach: 1. when a lib inherits another lib's functions, it doesn't automatically get put in to the scope of lib 2. when a lib implements a new obscure functions, it doesn't automatically get put in to the scope of lib Using the test script (later in this commit) I got the following diff on the API: + diff master fixed-lib 11764a11765,11766 > .types.defaultFunctor > .types.defaultTypeMerge 11774a11777,11778 > .types.isOptionType > .types.isType 11781a11786 > .types.mkOptionType 11788a11794 > .types.setType 11795a11802 > .types.types This means that this commit _adds_ to the API, however I can't find a way to fix these last remaining discrepancies. At least none are _removed_. Test script (run with nix-repl in the PATH): #!/bin/sh set -eux repl() { suff=${1:-} echo "(import ./lib)$suff" \ | nix-repl 2>&1 } attrs_to_check() { repl "${1:-}" \ | tr ';' $'\n' \ | grep "\.\.\." \ | cut -d' ' -f2 \ | sed -e "s/^/${1:-}./" \ | sort } summ() { repl "${1:-}" \ | tr ' ' $'\n' \ | sort \ | uniq } deep_summ() { suff="${1:-}" depth="${2:-4}" depth=$((depth - 1)) summ "$suff" for attr in $(attrs_to_check "$suff" | grep -v "types.types"); do if [ $depth -eq 0 ]; then summ "$attr" | sed -e "s/^/$attr./" else deep_summ "$attr" "$depth" | sed -e "s/^/$attr./" fi done } ( cd nixpkgs #git add . #git commit -m "Auto-commit, sorry" || true git checkout fixed-lib deep_summ > ../fixed-lib git checkout master deep_summ > ../master ) if diff master fixed-lib; then echo "SHALLOW MATCH!" fi ( cd nixpkgs git checkout fixed-lib repl .types ) (cherry picked from commit 152c63c)
1 parent db849d3 commit 97452cc

27 files changed

+209
-127
lines changed
 

‎lib/attrsets.nix

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
{ lib }:
12
# Operations on attribute sets.
23

34
let
45
inherit (builtins) head tail length;
5-
inherit (import ./trivial.nix) and or;
6-
inherit (import ./default.nix) fold;
7-
inherit (import ./strings.nix) concatStringsSep;
8-
inherit (import ./lists.nix) concatMap concatLists all deepSeqList;
6+
inherit (lib.trivial) and or;
7+
inherit (lib.strings) concatStringsSep;
8+
inherit (lib.lists) fold concatMap concatLists all deepSeqList;
99
in
1010

1111
rec {

‎lib/customisation.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
{ lib }:
12
let
23

3-
lib = import ./default.nix;
44
inherit (builtins) attrNames isFunction;
55

66
in

‎lib/debug.nix

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
let lib = import ./default.nix;
1+
{ lib }:
2+
3+
let
24

35
inherit (builtins) trace attrNamesToStr isAttrs isFunction isList isInt
46
isString isBool head substring attrNames;

‎lib/default.nix

+121-52
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,127 @@
55
*/
66
let
77

8-
# often used, or depending on very little
9-
trivial = import ./trivial.nix;
10-
fixedPoints = import ./fixed-points.nix;
11-
12-
# datatypes
13-
attrsets = import ./attrsets.nix;
14-
lists = import ./lists.nix;
15-
strings = import ./strings.nix;
16-
stringsWithDeps = import ./strings-with-deps.nix;
17-
18-
# packaging
19-
customisation = import ./customisation.nix;
20-
maintainers = import ./maintainers.nix;
21-
meta = import ./meta.nix;
22-
sources = import ./sources.nix;
23-
24-
# module system
25-
modules = import ./modules.nix;
26-
options = import ./options.nix;
27-
types = import ./types.nix;
28-
29-
# constants
30-
licenses = import ./licenses.nix;
31-
systems = import ./systems;
32-
33-
# misc
34-
debug = import ./debug.nix;
35-
generators = import ./generators.nix;
36-
misc = import ./deprecated.nix;
37-
38-
# domain-specific
39-
sandbox = import ./sandbox.nix;
40-
fetchers = import ./fetchers.nix;
41-
42-
# Eval-time filesystem handling
43-
filesystem = import ./filesystem.nix;
44-
45-
in
46-
{ inherit trivial fixedPoints
47-
attrsets lists strings stringsWithDeps
48-
customisation maintainers meta sources
49-
modules options types
50-
licenses systems
51-
debug generators misc
52-
sandbox fetchers filesystem;
8+
callLibs = file: import file { inherit lib; };
9+
10+
lib = rec {
11+
12+
# often used, or depending on very little
13+
trivial = callLibs ./trivial.nix;
14+
fixedPoints = callLibs ./fixed-points.nix;
15+
16+
# datatypes
17+
attrsets = callLibs ./attrsets.nix;
18+
lists = callLibs ./lists.nix;
19+
strings = callLibs ./strings.nix;
20+
stringsWithDeps = callLibs ./strings-with-deps.nix;
21+
22+
# packaging
23+
customisation = callLibs ./customisation.nix;
24+
maintainers = callLibs ./maintainers.nix;
25+
meta = callLibs ./meta.nix;
26+
sources = callLibs ./sources.nix;
27+
28+
29+
# module system
30+
modules = callLibs ./modules.nix;
31+
options = callLibs ./options.nix;
32+
types = callLibs ./types.nix;
33+
34+
# constants
35+
licenses = callLibs ./licenses.nix;
36+
systems = callLibs ./systems;
37+
38+
# misc
39+
debug = callLibs ./debug.nix;
40+
41+
generators = callLibs ./generators.nix;
42+
misc = callLibs ./deprecated.nix;
43+
# domain-specific
44+
sandbox = callLibs ./sandbox.nix;
45+
fetchers = callLibs ./fetchers.nix;
46+
47+
# Eval-time filesystem handling
48+
filesystem = callLibs ./filesystem.nix;
5349

5450
# back-compat aliases
5551
platforms = systems.doubles;
56-
}
57-
# !!! don't include everything at top-level; perhaps only the most
58-
# commonly used functions.
59-
// trivial // fixedPoints
60-
// lists // strings // stringsWithDeps // attrsets // sources
61-
// options // types // meta // debug // misc // modules
62-
// customisation
52+
53+
inherit (builtins) add addErrorContext attrNames
54+
concatLists deepSeq elem elemAt filter genericClosure genList
55+
getAttr hasAttr head isAttrs isBool isFunction isInt isList
56+
isString length lessThan listToAttrs pathExists readFile
57+
replaceStrings seq stringLength sub substring tail;
58+
inherit (trivial) id const concat or and boolToString mergeAttrs
59+
flip mapNullable inNixShell min max importJSON warn info
60+
nixpkgsVersion mod;
61+
62+
inherit (fixedPoints) fix fix' extends composeExtensions
63+
makeExtensible makeExtensibleWithCustomName;
64+
inherit (attrsets) attrByPath hasAttrByPath setAttrByPath
65+
getAttrFromPath attrVals attrValues catAttrs filterAttrs
66+
filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs
67+
mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond
68+
genAttrs isDerivation toDerivation optionalAttrs
69+
zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
70+
recursiveUpdate matchAttrs overrideExisting getOutput getBin
71+
getLib getDev chooseDevOutputs zipWithNames zip;
72+
inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
73+
concatMap flatten remove findSingle findFirst any all count
74+
optional optionals toList range partition zipListsWith zipLists
75+
reverseList listDfs toposort sort take drop sublist last init
76+
crossLists unique intersectLists subtractLists
77+
mutuallyExclusive;
78+
inherit (strings) concatStrings concatMapStrings concatImapStrings
79+
intersperse concatStringsSep concatMapStringsSep
80+
concatImapStringsSep makeSearchPath makeSearchPathOutput
81+
makeLibraryPath makeBinPath makePerlPath optionalString
82+
hasPrefix hasSuffix stringToCharacters stringAsChars escape
83+
escapeShellArg escapeShellArgs replaceChars lowerChars upperChars
84+
toLower toUpper addContextFrom splitString removePrefix
85+
removeSuffix versionOlder versionAtLeast getVersion nameFromURL
86+
enableFeature fixedWidthString fixedWidthNumber isStorePath
87+
toInt readPathsFromFile fileContents;
88+
inherit (stringsWithDeps) textClosureList textClosureMap
89+
noDepEntry fullDepEntry packEntry stringAfter;
90+
inherit (customisation) overrideDerivation makeOverridable
91+
callPackageWith callPackagesWith addPassthru hydraJob makeScope;
92+
inherit (meta) addMetaAttrs dontDistribute setName updateName
93+
appendToName mapDerivationAttrset lowPrio lowPrioSet hiPrio
94+
hiPrioSet;
95+
inherit (sources) pathType pathIsDirectory cleanSourceFilter
96+
cleanSource sourceByRegex sourceFilesBySuffices
97+
commitIdFromGitRepo;
98+
inherit (modules) evalModules closeModules unifyModuleSyntax
99+
applyIfFunction unpackSubmodule packSubmodule mergeModules
100+
mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions
101+
pushDownProperties dischargeProperties filterOverrides
102+
sortProperties fixupOptionType mkIf mkAssert mkMerge mkOverride
103+
mkOptionDefault mkDefault mkForce mkVMOverride mkStrict
104+
mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions
105+
mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule
106+
mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule
107+
mkAliasOptionModule doRename filterModules;
108+
inherit (options) isOption mkEnableOption mkSinkUndeclaredOptions
109+
mergeDefaultOption mergeOneOption mergeEqualOption getValues
110+
getFiles optionAttrSetToDocList optionAttrSetToDocList'
111+
scrubOptionValue literalExample showOption showFiles
112+
unknownModule mkOption;
113+
inherit (types) isType setType defaultTypeMerge defaultFunctor
114+
isOptionType mkOptionType;
115+
inherit (debug) addErrorContextToAttrs traceIf traceVal
116+
traceXMLVal traceXMLValMarked traceSeq traceSeqN traceValSeq
117+
traceValSeqN traceShowVal traceShowValMarked
118+
showVal traceCall traceCall2 traceCall3 traceValIfNot runTests
119+
testAllTrue strict traceCallXml attrNamesToStr;
120+
inherit (misc) maybeEnv defaultMergeArg defaultMerge foldArgs
121+
defaultOverridableDelayableArgs composedArgsAndFun
122+
maybeAttrNullable maybeAttr ifEnable checkFlag getValue
123+
checkReqs uniqList uniqListExt condConcat lazyGenericClosure
124+
innerModifySumArgs modifySumArgs innerClosePropagation
125+
closePropagation mapAttrsFlatten nvs setAttr setAttrMerge
126+
mergeAttrsWithFunc mergeAttrsConcatenateValues
127+
mergeAttrsNoOverride mergeAttrByFunc mergeAttrsByFuncDefaults
128+
mergeAttrsByFuncDefaultsClean mergeAttrBy
129+
prepareDerivationArgs nixType imap overridableDelayableArgs;
130+
};
131+
in lib

‎lib/deprecated.nix

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
let lib = import ./default.nix;
1+
{ lib }:
2+
let
23
inherit (builtins) isFunction head tail isList isAttrs isInt attrNames;
34

45
in
56

6-
with import ./lists.nix;
7-
with import ./attrsets.nix;
8-
with import ./strings.nix;
7+
with lib.lists;
8+
with lib.attrsets;
9+
with lib.strings;
910

1011
rec {
1112

‎lib/fetchers.nix

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# snippets that can be shared by multiple fetchers (pkgs/build-support)
2+
{ lib }:
23
{
34

45
proxyImpureEnvVars = [

‎lib/filesystem.nix

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ lib }:
12
{ # haskellPathsInDir : Path -> Map String Path
23
# A map of all haskell packages defined in the given path,
34
# identified by having a cabal file with the same name as the

‎lib/fixed-points.nix

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ ... }:
12
rec {
23
# Compute the fixed point of the given function `f`, which is usually an
34
# attribute set that expects its final, non-recursive representation as an

‎lib/generators.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
* Tests can be found in ./tests.nix
88
* Documentation in the manual, #sec-generators
99
*/
10-
with import ./trivial.nix;
10+
{ lib }:
11+
with (lib).trivial;
1112
let
12-
libStr = import ./strings.nix;
13-
libAttr = import ./attrsets.nix;
13+
libStr = lib.strings;
14+
libAttr = lib.attrsets;
1415

1516
flipMapAttrs = flip libAttr.mapAttrs;
1617
in

‎lib/licenses.nix

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
{ lib }:
12
let
23

3-
lib = import ./default.nix;
4-
54
spdx = lic: lic // {
65
url = "http://spdx.org/licenses/${lic.spdxId}";
76
};
@@ -419,7 +418,7 @@ lib.mapAttrs (n: v: v // { shortName = n; }) rec {
419418
url = "https://raw.githubusercontent.com/raboof/notion/master/LICENSE";
420419
fullName = "Notion modified LGPL";
421420
};
422-
421+
423422
ofl = spdx {
424423
spdxId = "OFL-1.1";
425424
fullName = "SIL Open Font License 1.1";

‎lib/lists.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# General list operations.
2-
3-
with import ./trivial.nix;
2+
{ lib }:
3+
with lib.trivial;
44

55
rec {
66

‎lib/maintainers.nix

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ ...}:
12
/* List of NixOS maintainers. The format is:
23
34
handle = "Real Name <address@example.org>";

‎lib/meta.nix

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* Some functions for manipulating meta attributes, as well as the
22
name attribute. */
33

4-
let lib = import ./default.nix;
5-
in
4+
{ lib }:
65

76
rec {
87

‎lib/modules.nix

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
with import ./lists.nix;
2-
with import ./strings.nix;
3-
with import ./trivial.nix;
4-
with import ./attrsets.nix;
5-
with import ./options.nix;
6-
with import ./debug.nix;
7-
with import ./types.nix;
1+
{ lib }:
2+
3+
with lib.lists;
4+
with lib.strings;
5+
with lib.trivial;
6+
with lib.attrsets;
7+
with lib.options;
8+
with lib.debug;
9+
with lib.types;
810

911
rec {
1012

‎lib/options.nix

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# Nixpkgs/NixOS option handling.
2+
{ lib }:
23

3-
let lib = import ./default.nix; in
4-
5-
with import ./trivial.nix;
6-
with import ./lists.nix;
7-
with import ./attrsets.nix;
8-
with import ./strings.nix;
4+
with lib.trivial;
5+
with lib.lists;
6+
with lib.attrsets;
7+
with lib.strings;
98

109
rec {
1110

‎lib/sandbox.nix

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
with import ./strings.nix;
1+
{ lib }:
2+
with lib.strings;
23

34
/* Helpers for creating lisp S-exprs for the Apple sandbox
45

‎lib/sources.nix

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Functions for copying sources to the Nix store.
2-
3-
let lib = import ./default.nix; in
2+
{ lib }:
43

54
rec {
65

‎lib/strings-with-deps.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ lib }:
12
/*
23
Usage:
34
@@ -40,9 +41,9 @@ Usage:
4041
[1] maybe this behaviour should be removed to keep things simple (?)
4142
*/
4243

43-
with import ./lists.nix;
44-
with import ./attrsets.nix;
45-
with import ./strings.nix;
44+
with lib.lists;
45+
with lib.attrsets;
46+
with lib.strings;
4647

4748
rec {
4849

‎lib/strings.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* String manipulation functions. */
2-
3-
let lib = import ./default.nix;
2+
{ lib }:
3+
let
44

55
inherit (builtins) length;
66

‎lib/systems/default.nix

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
let inherit (import ../attrsets.nix) mapAttrs; in
1+
{ lib }:
2+
let inherit (lib.attrsets) mapAttrs; in
23

34
rec {
4-
doubles = import ./doubles.nix;
5-
parse = import ./parse.nix;
6-
inspect = import ./inspect.nix;
7-
platforms = import ./platforms.nix;
8-
examples = import ./examples.nix;
5+
doubles = import ./doubles.nix { inherit lib; };
6+
parse = import ./parse.nix { inherit lib; };
7+
inspect = import ./inspect.nix { inherit lib; };
8+
platforms = import ./platforms.nix { inherit lib; };
9+
examples = import ./examples.nix { inherit lib; };
910

1011
# Elaborate a `localSystem` or `crossSystem` so that it contains everything
1112
# necessary.

‎lib/systems/doubles.nix

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
{ lib }:
12
let
2-
lists = import ../lists.nix;
3-
parse = import ./parse.nix;
4-
inherit (import ./inspect.nix) predicates;
5-
inherit (import ../attrsets.nix) matchAttrs;
3+
inherit (lib) lists;
4+
parse = import ./parse.nix { inherit lib; };
5+
inherit (import ./inspect.nix { inherit lib; }) predicates;
6+
inherit (lib.attrsets) matchAttrs;
67

78
all = [
89
"aarch64-linux"

‎lib/systems/examples.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# These can be passed to nixpkgs as either the `localSystem` or
22
# `crossSystem`. They are put here for user convenience, but also used by cross
33
# tests and linux cross stdenv building, so handle with care!
4-
5-
let platforms = import ./platforms.nix; in
4+
{ lib }:
5+
let platforms = import ./platforms.nix { inherit lib; }; in
66

77
rec {
88
#

‎lib/systems/inspect.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
with import ./parse.nix;
2-
with import ../attrsets.nix;
3-
with import ../lists.nix;
1+
{ lib }:
2+
with import ./parse.nix { inherit lib; };
3+
with lib.attrsets;
4+
with lib.lists;
45

56
rec {
67
patterns = rec {

‎lib/systems/parse.nix

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
# http://llvm.org/docs/doxygen/html/Triple_8cpp_source.html especially
55
# Triple::normalize. Parsing should essentially act as a more conservative
66
# version of that last function.
7-
8-
with import ../lists.nix;
9-
with import ../types.nix;
10-
with import ../attrsets.nix;
11-
with (import ./inspect.nix).predicates;
7+
{ lib }:
8+
with lib.lists;
9+
with lib.types;
10+
with lib.attrsets;
11+
with (import ./inspect.nix { inherit lib; }).predicates;
1212

1313
let
14-
lib = import ../default.nix;
1514
setTypesAssert = type: pred:
1615
mapAttrs (name: value:
1716
assert pred value;

‎lib/systems/platforms.nix

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ lib }:
12
rec {
23
pcBase = {
34
name = "pc";

‎lib/trivial.nix

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ lib }:
12
rec {
23

34
/* The identity function
@@ -55,7 +56,7 @@ rec {
5556
isInt add sub lessThan
5657
seq deepSeq genericClosure;
5758

58-
inherit (import ./strings.nix) fileContents;
59+
inherit (lib.strings) fileContents;
5960

6061
# Return the Nixpkgs version number.
6162
nixpkgsVersion =

‎lib/types.nix

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Definitions related to run-time type checking. Used in particular
22
# to type-check NixOS configurations.
3-
4-
with import ./lists.nix;
5-
with import ./attrsets.nix;
6-
with import ./options.nix;
7-
with import ./trivial.nix;
8-
with import ./strings.nix;
9-
let inherit (import ./modules.nix) mergeDefinitions filterOverrides; in
10-
3+
{ lib }:
4+
with lib.lists;
5+
with lib.attrsets;
6+
with lib.options;
7+
with lib.trivial;
8+
with lib.strings;
9+
let
10+
11+
inherit (lib.modules) mergeDefinitions filterOverrides;
12+
outer_types =
1113
rec {
12-
1314
isType = type: x: (x._type or "") == type;
1415

1516
setType = typeName: value: value // {
@@ -95,7 +96,6 @@ rec {
9596
# When adding new types don't forget to document them in
9697
# nixos/doc/manual/development/option-types.xml!
9798
types = rec {
98-
9999
unspecified = mkOptionType {
100100
name = "unspecified";
101101
};
@@ -291,7 +291,7 @@ rec {
291291
submodule = opts:
292292
let
293293
opts' = toList opts;
294-
inherit (import ./modules.nix) evalModules;
294+
inherit (lib.modules) evalModules;
295295
in
296296
mkOptionType rec {
297297
name = "submodule";
@@ -395,5 +395,6 @@ rec {
395395
addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; };
396396

397397
};
398+
};
398399

399-
}
400+
in outer_types // outer_types.types

0 commit comments

Comments
 (0)
Please sign in to comment.