Skip to content

Commit 5f667de

Browse files
committed
treewide: Format all Nix files
Format all Nix files using the officially approved formatter, making the CI check introduced in the previous commit succeed: nix-build ci -A fmt.check This is the next step of the of the [implementation](NixOS/nixfmt#153) of the accepted [RFC 166](NixOS/rfcs#166). This commit will lead to merge conflicts for a number of PRs, up to an estimated ~1100 (~33%) among the PRs with activity in the past 2 months, but that should be lower than what it would be without the previous [partial treewide format](NixOS/nixpkgs#322537). Merge conflicts caused by this commit can now automatically be resolved while rebasing using the [auto-rebase script](https://github.com/NixOS/nixpkgs/tree/8616af08d915377bd930395f3b700a0e93d08728/maintainers/scripts/auto-rebase). If you run into any problems regarding any of this, please reach out to the [formatting team](https://nixos.org/community/teams/formatting/) by pinging @NixOS/nix-formatting.
1 parent 89e4630 commit 5f667de

24 files changed

Lines changed: 10831 additions & 6931 deletions

lib/attrsets.nix

Lines changed: 268 additions & 325 deletions
Large diffs are not rendered by default.

lib/default.nix

Lines changed: 560 additions & 206 deletions
Large diffs are not rendered by default.

lib/deprecated/misc.nix

Lines changed: 310 additions & 196 deletions
Large diffs are not rendered by default.

lib/filesystem.nix

Lines changed: 109 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,26 @@ in
5959
pathType =
6060
builtins.readFileType or
6161
# Nix <2.14 compatibility shim
62-
(path:
63-
if ! pathExists path
62+
(
63+
path:
64+
if
65+
!pathExists path
6466
# Fail irrecoverably to mimic the historic behavior of this function and
6567
# the new builtins.readFileType
66-
then abort "lib.filesystem.pathType: Path ${toString path} does not exist."
68+
then
69+
abort "lib.filesystem.pathType: Path ${toString path} does not exist."
6770
# The filesystem root is the only path where `dirOf / == /` and
6871
# `baseNameOf /` is not valid. We can detect this and directly return
6972
# "directory", since we know the filesystem root can't be anything else.
70-
else if dirOf path == path
71-
then "directory"
72-
else (readDir (dirOf path)).${baseNameOf path}
73+
else if dirOf path == path then
74+
"directory"
75+
else
76+
(readDir (dirOf path)).${baseNameOf path}
7377
);
7478

7579
/**
7680
Whether a path exists and is a directory.
7781
78-
7982
# Inputs
8083
8184
`path`
@@ -105,13 +108,11 @@ in
105108
106109
:::
107110
*/
108-
pathIsDirectory = path:
109-
pathExists path && pathType path == "directory";
111+
pathIsDirectory = path: pathExists path && pathType path == "directory";
110112

111113
/**
112114
Whether a path exists and is a regular file, meaning not a symlink or any other special file type.
113115
114-
115116
# Inputs
116117
117118
`path`
@@ -141,15 +142,13 @@ in
141142
142143
:::
143144
*/
144-
pathIsRegularFile = path:
145-
pathExists path && pathType path == "regular";
145+
pathIsRegularFile = path: pathExists path && pathType path == "regular";
146146

147147
/**
148148
A map of all haskell packages defined in the given path,
149149
identified by having a cabal file with the same name as the
150150
directory itself.
151151
152-
153152
# Inputs
154153
155154
`root`
@@ -164,25 +163,25 @@ in
164163
*/
165164
haskellPathsInDir =
166165
root:
167-
let # Files in the root
168-
root-files = builtins.attrNames (builtins.readDir root);
169-
# Files with their full paths
170-
root-files-with-paths =
171-
map (file:
172-
{ name = file; value = root + "/${file}"; }
173-
) root-files;
174-
# Subdirectories of the root with a cabal file.
175-
cabal-subdirs =
176-
builtins.filter ({ name, value }:
177-
builtins.pathExists (value + "/${name}.cabal")
178-
) root-files-with-paths;
179-
in builtins.listToAttrs cabal-subdirs;
166+
let
167+
# Files in the root
168+
root-files = builtins.attrNames (builtins.readDir root);
169+
# Files with their full paths
170+
root-files-with-paths = map (file: {
171+
name = file;
172+
value = root + "/${file}";
173+
}) root-files;
174+
# Subdirectories of the root with a cabal file.
175+
cabal-subdirs = builtins.filter (
176+
{ name, value }: builtins.pathExists (value + "/${name}.cabal")
177+
) root-files-with-paths;
178+
in
179+
builtins.listToAttrs cabal-subdirs;
180180
/**
181181
Find the first directory containing a file matching 'pattern'
182182
upward from a given 'file'.
183183
Returns 'null' if no directories contain a file matching 'pattern'.
184184
185-
186185
# Inputs
187186
188187
`pattern`
@@ -200,30 +199,33 @@ in
200199
```
201200
*/
202201
locateDominatingFile =
203-
pattern:
204-
file:
205-
let go = path:
206-
let files = builtins.attrNames (builtins.readDir path);
207-
matches = builtins.filter (match: match != null)
208-
(map (builtins.match pattern) files);
209-
in
210-
if builtins.length matches != 0
211-
then { inherit path matches; }
212-
else if path == /.
213-
then null
214-
else go (dirOf path);
215-
parent = dirOf file;
216-
isDir =
217-
let base = baseNameOf file;
218-
type = (builtins.readDir parent).${base} or null;
219-
in file == /. || type == "directory";
220-
in go (if isDir then file else parent);
221-
202+
pattern: file:
203+
let
204+
go =
205+
path:
206+
let
207+
files = builtins.attrNames (builtins.readDir path);
208+
matches = builtins.filter (match: match != null) (map (builtins.match pattern) files);
209+
in
210+
if builtins.length matches != 0 then
211+
{ inherit path matches; }
212+
else if path == /. then
213+
null
214+
else
215+
go (dirOf path);
216+
parent = dirOf file;
217+
isDir =
218+
let
219+
base = baseNameOf file;
220+
type = (builtins.readDir parent).${base} or null;
221+
in
222+
file == /. || type == "directory";
223+
in
224+
go (if isDir then file else parent);
222225

223226
/**
224227
Given a directory, return a flattened list of all files within it recursively.
225228
226-
227229
# Inputs
228230
229231
`dir`
@@ -238,12 +240,15 @@ in
238240
*/
239241
listFilesRecursive =
240242
dir:
241-
lib.flatten (lib.mapAttrsToList (name: type:
242-
if type == "directory" then
243-
lib.filesystem.listFilesRecursive (dir + "/${name}")
244-
else
245-
dir + "/${name}"
246-
) (builtins.readDir dir));
243+
lib.flatten (
244+
lib.mapAttrsToList (
245+
name: type:
246+
if type == "directory" then
247+
lib.filesystem.listFilesRecursive (dir + "/${name}")
248+
else
249+
dir + "/${name}"
250+
) (builtins.readDir dir)
251+
);
247252

248253
/**
249254
Transform a directory tree containing package files suitable for
@@ -373,29 +378,49 @@ in
373378
*/
374379
packagesFromDirectoryRecursive =
375380
let
376-
inherit (lib) concatMapAttrs id makeScope recurseIntoAttrs removeSuffix;
381+
inherit (lib)
382+
concatMapAttrs
383+
id
384+
makeScope
385+
recurseIntoAttrs
386+
removeSuffix
387+
;
377388
inherit (lib.path) append;
378389

379390
# Generate an attrset corresponding to a given directory.
380391
# This function is outside `packagesFromDirectoryRecursive`'s lambda expression,
381392
# to prevent accidentally using its parameters.
382-
processDir = { callPackage, directory, ... }@args:
383-
concatMapAttrs (name: type:
393+
processDir =
394+
{ callPackage, directory, ... }@args:
395+
concatMapAttrs (
396+
name: type:
384397
# for each directory entry
385-
let path = append directory name; in
386-
if type == "directory" then {
387-
# recurse into directories
388-
"${name}" = packagesFromDirectoryRecursive (args // {
389-
directory = path;
390-
});
391-
} else if type == "regular" && hasSuffix ".nix" name then {
392-
# call .nix files
393-
"${removeSuffix ".nix" name}" = callPackage path {};
394-
} else if type == "regular" then {
395-
# ignore non-nix files
396-
} else throw ''
397-
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
398-
''
398+
let
399+
path = append directory name;
400+
in
401+
if type == "directory" then
402+
{
403+
# recurse into directories
404+
"${name}" = packagesFromDirectoryRecursive (
405+
args
406+
// {
407+
directory = path;
408+
}
409+
);
410+
}
411+
else if type == "regular" && hasSuffix ".nix" name then
412+
{
413+
# call .nix files
414+
"${removeSuffix ".nix" name}" = callPackage path { };
415+
}
416+
else if type == "regular" then
417+
{
418+
# ignore non-nix files
419+
}
420+
else
421+
throw ''
422+
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
423+
''
399424
) (builtins.readDir directory);
400425
in
401426
{
@@ -408,20 +433,25 @@ in
408433
in
409434
if pathExists defaultPath then
410435
# if `${directory}/package.nix` exists, call it directly
411-
callPackage defaultPath {}
436+
callPackage defaultPath { }
412437
else if args ? newScope then
413438
# Create a new scope and mark it `recurseForDerivations`.
414439
# This lets the packages refer to each other.
415440
# See:
416441
# [lib.makeScope](https://nixos.org/manual/nixpkgs/unstable/#function-library-lib.customisation.makeScope) and
417442
# [lib.recurseIntoAttrs](https://nixos.org/manual/nixpkgs/unstable/#function-library-lib.customisation.makeScope)
418-
recurseIntoAttrs (makeScope newScope (self:
419-
# generate the attrset representing the directory, using the new scope's `callPackage` and `newScope`
420-
processDir (args // {
421-
inherit (self) callPackage newScope;
422-
})
423-
))
443+
recurseIntoAttrs (
444+
makeScope newScope (
445+
self:
446+
# generate the attrset representing the directory, using the new scope's `callPackage` and `newScope`
447+
processDir (
448+
args
449+
// {
450+
inherit (self) callPackage newScope;
451+
}
452+
)
453+
)
454+
)
424455
else
425-
processDir args
426-
;
456+
processDir args;
427457
}

0 commit comments

Comments
 (0)