Skip to content

Commit

Permalink
Format Nix files treewide
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Apr 17, 2024
1 parent bf57bd2 commit 81d712a
Show file tree
Hide file tree
Showing 35 changed files with 160 additions and 194 deletions.
51 changes: 29 additions & 22 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ in
let
pkgs = import nixpkgs {
inherit system;
config = {};
overlays = [];
config = { };
overlays = [ ];
};
inherit (pkgs) lib;

Expand Down Expand Up @@ -40,14 +40,21 @@ let
projectRootFile = ".git/config";

programs.rustfmt.enable = true;
programs.nixfmt-rfc-style.enable = true;
};

results = {
# We're using this value as the root result. By default, derivations expose all of their
# internal attributes, which is very messy. We prevent this using lib.lazyDerivation
build = lib.lazyDerivation {
derivation = pkgs.callPackage ./package.nix {
inherit nixpkgsLibPath initNix runtimeExprPath testNixpkgsPath version;
inherit
nixpkgsLibPath
initNix
runtimeExprPath
testNixpkgsPath
version
;
};
};

Expand Down Expand Up @@ -76,9 +83,7 @@ let
updateScripts = {
npins = pkgs.writeShellApplication {
name = "update-npins";
runtimeInputs = with pkgs; [
npins
];
runtimeInputs = with pkgs; [ npins ];
text = ''
echo "<details><summary>npins changes</summary>"
# Needed because GitHub's rendering of the first body line breaks down otherwise
Expand All @@ -91,9 +96,7 @@ let
};
cargo = pkgs.writeShellApplication {
name = "update-cargo";
runtimeInputs = with pkgs; [
cargo
];
runtimeInputs = with pkgs; [ cargo ];
text = ''
echo "<details><summary>cargo changes</summary>"
# Needed because GitHub's rendering of the first body line breaks down otherwise
Expand Down Expand Up @@ -129,21 +132,25 @@ let
};

# Tests the tool on the pinned Nixpkgs tree, this is a good sanity check
nixpkgsCheck = pkgs.runCommand "test-nixpkgs-check-by-name" {
nativeBuildInputs = [
results.build
pkgs.nix
];
nixpkgsPath = nixpkgs;
} ''
${initNix}
nixpkgs-check-by-name --base "$nixpkgsPath" "$nixpkgsPath"
touch $out
'';
nixpkgsCheck =
pkgs.runCommand "test-nixpkgs-check-by-name"
{
nativeBuildInputs = [
results.build
pkgs.nix
];
nixpkgsPath = nixpkgs;
}
''
${initNix}
nixpkgs-check-by-name --base "$nixpkgsPath" "$nixpkgsPath"
touch $out
'';
};

in
results.build // results // {
results.build
// results
// {

# Good for debugging
inherit pkgs;
Expand Down
48 changes: 33 additions & 15 deletions npins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@ let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;

mkSource = spec:
assert spec ? type; let
mkSource =
spec:
assert spec ? type;
let
path =
if spec.type == "Git" then mkGitSource spec
else if spec.type == "GitRelease" then mkGitSource spec
else if spec.type == "PyPi" then mkPyPiSource spec
else if spec.type == "Channel" then mkChannelSource spec
else builtins.throw "Unknown source type ${spec.type}";
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };

mkGitSource = { repository, revision, url ? null, hash, ... }:
mkGitSource =
{
repository,
revision,
url ? null,
hash,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
Expand All @@ -23,19 +37,23 @@ let
inherit url;
sha256 = hash; # FIXME: check nix version & use SRI hashes
})
else assert repository.type == "Git"; builtins.fetchGit {
url = repository.url;
rev = revision;
# hash = hash;
};
else
assert repository.type == "Git";
builtins.fetchGit {
url = repository.url;
rev = revision;
# hash = hash;
};

mkPyPiSource = { url, hash, ... }:
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};

mkChannelSource = { url, hash, ... }:
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
Expand Down
1 change: 0 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ rustPlatform.buildRustPackage {
wrapProgram $out/bin/nixpkgs-check-by-name \
--set NIX_CHECK_BY_NAME_EXPR_PATH "$NIX_CHECK_BY_NAME_EXPR_PATH"
'';

}
55 changes: 24 additions & 31 deletions src/eval.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# Returns a value containing information on all Nixpkgs attributes
# which is decoded on the Rust side.
# See ./eval.rs for the meaning of the returned values
{
attrsPath,
nixpkgsPath,
}:
{ attrsPath, nixpkgsPath }:
let
attrs = builtins.fromJSON (builtins.readFile attrsPath);

Expand All @@ -17,7 +14,8 @@ let
overlay = final: prev: {

# Adds information to each attribute about whether it's manually defined using `callPackage`
callPackage = fn: args:
callPackage =
fn: args:
addVariantInfo (prev.callPackage fn args) {
# This is a manual definition of the attribute, and it's a callPackage, specifically a semantic callPackage
ManualDefinition.is_semantic_call_package = true;
Expand All @@ -28,20 +26,16 @@ let
# used by that overlay.
# This overrides the above `callPackage` information (we don't need that
# one, since `pkgs/by-name` always uses `callPackage` underneath.
_internalCallByNamePackageFile = file:
addVariantInfo (prev._internalCallByNamePackageFile file) {
AutoDefinition = null;
};

_internalCallByNamePackageFile =
file: addVariantInfo (prev._internalCallByNamePackageFile file) { AutoDefinition = null; };
};

# We can't just replace attribute values with their info in the overlay,
# because attributes can depend on other attributes, so this would break evaluation.
addVariantInfo = value: variant:
addVariantInfo =
value: variant:
if builtins.isAttrs value then
value // {
_callPackageVariant = variant;
}
value // { _callPackageVariant = variant; }
else
# It's very rare that callPackage doesn't return an attribute set, but it can occur.
# In such a case we can't really return anything sensible that would include the info,
Expand All @@ -61,14 +55,14 @@ let
attrInfo = name: value: {
location = builtins.unsafeGetAttrPos name pkgs;
attribute_variant =
if ! builtins.isAttrs value then
if !builtins.isAttrs value then
{ NonAttributeSet = null; }
else
{
AttributeSet = {
is_derivation = pkgs.lib.isDerivation value;
definition_variant =
if ! value ? _callPackageVariant then
if !value ? _callPackageVariant then
{ ManualDefinition.is_semantic_call_package = false; }
else
value._callPackageVariant;
Expand All @@ -77,31 +71,30 @@ let
};

# Information on all attributes that are in pkgs/by-name.
byNameAttrs = builtins.listToAttrs (map (name: {
inherit name;
value.ByName =
if ! pkgs ? ${name} then
{ Missing = null; }
else
# Evaluation failures are not allowed, so don't try to catch them
{ Existing = attrInfo name pkgs.${name}; };
}) attrs);
byNameAttrs = builtins.listToAttrs (
map (name: {
inherit name;
value.ByName =
if !pkgs ? ${name} then
{ Missing = null; }
else
# Evaluation failures are not allowed, so don't try to catch them
{ Existing = attrInfo name pkgs.${name}; };
}) attrs
);

# Information on all attributes that exist but are not in pkgs/by-name.
# We need this to enforce pkgs/by-name for new packages
nonByNameAttrs = builtins.mapAttrs (name: value:
nonByNameAttrs = builtins.mapAttrs (
name: value:
let
# Packages outside `pkgs/by-name` often fail evaluation,
# so we need to handle that
output = attrInfo name value;
result = builtins.tryEval (builtins.deepSeq output null);
in
{
NonByName =
if result.success then
{ EvalSuccess = output; }
else
{ EvalFailure = null; };
NonByName = if result.success then { EvalSuccess = output; } else { EvalFailure = null; };
}
) (builtins.removeAttrs pkgs attrs);

Expand Down
4 changes: 1 addition & 3 deletions tests/aliases/aliases.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
self: super: {
baz = self.foo;
}
self: super: { baz = self.foo; }
4 changes: 1 addition & 3 deletions tests/aliases/pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
self: super: {
bar = self.foo;
}
self: super: { bar = self.foo; }
3 changes: 1 addition & 2 deletions tests/alt-callPackage/pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
self: super: {

alt.callPackage = self.lib.callPackageWith {};
alt.callPackage = self.lib.callPackageWith { };

foo = self.alt.callPackage ({ }: self.someDrv) { };

}
5 changes: 1 addition & 4 deletions tests/broken-autocall/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
args:
builtins.removeAttrs
(import <test-nixpkgs> { root = ./.; } args)
[ "foo" ]
args: builtins.removeAttrs (import <test-nixpkgs> { root = ./.; } args) [ "foo" ]
3 changes: 1 addition & 2 deletions tests/by-name-failure/pkgs/by-name/fo/foo/package.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{ }:
# If we caused an actual Nix failure
builtins.trace "This should be on stderr!"
throw "This is an error!"
builtins.trace "This should be on stderr!" throw "This is an error!"
9 changes: 6 additions & 3 deletions tests/callPackage-syntax/pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
self: super: {
set = self.callPackages ({ callPackage }: {
foo = callPackage ({ someDrv }: someDrv) { };
}) { };
set = self.callPackages (
{ callPackage }:
{
foo = callPackage ({ someDrv }: someDrv) { };
}
) { };

inherit (self.set) foo;
}
Loading

0 comments on commit 81d712a

Please sign in to comment.