Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.attrsets: Correct wrong documentation in getLib etc #286518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gabyx
Copy link
Contributor

@gabyx gabyx commented Feb 5, 2024

  • Functions do not return a String but a Derivation.

Description of changes

Correct some small documentation issues.


Add a 👍 reaction to pull requests you find important.

@gabyx gabyx requested a review from infinisil as a code owner February 5, 2024 15:51
@github-actions github-actions bot added the 6.topic: lib The Nixpkgs function library label Feb 5, 2024
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/understanding-lib-attrsets-getlib/39324/3

@tobiasBora
Copy link
Contributor

I think in the examples you want to add .drv at the end of all derivations:

nix-repl> lib.getLib pkgs.openssl     
«derivation /nix/store/plq8ixzschp4njfz95l0pvghx859y6qk-openssl-3.0.7.drv»

lib/attrsets.nix Outdated Show resolved Hide resolved
@@ -1070,30 +1070,29 @@ rec {
else concatMapStringsSep "." escapeNixIdentifier path;


/* Get a package output.
/* Get the output derivation with specific name of a package.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and output derivation is not a thing either. Closest is probably a “derivation output” but that is not a special thing. Really, you get the same store derivation either way – see the following pure Nix expressions (no Nixpkgs):

nix-repl> foo = (derivation { name = "foo"; outputs = ["out" "dummy"]; builder = "something"; system = builtins.currentSystem; })

nix-repl> foo
«derivation /nix/store/9h8ldpf7qihnk4fyzj6jfbl4gpsr3k9i-foo.drv»

nix-repl> foo.out
«derivation /nix/store/9h8ldpf7qihnk4fyzj6jfbl4gpsr3k9i-foo.drv»

nix-repl> foo.dummy
«derivation /nix/store/9h8ldpf7qihnk4fyzj6jfbl4gpsr3k9i-foo.drv»

The store derivation contains all outputs:

$ nix derivation show /nix/store/9h8ldpf7qihnk4fyzj6jfbl4gpsr3k9i-foo.drv
{
  "/nix/store/9h8ldpf7qihnk4fyzj6jfbl4gpsr3k9i-foo.drv": {
    "args": [],
    "builder": "something",
    "env": {
      "builder": "something",
      "dummy": "/nix/store/ssv0b9s9w3zw4br6zhghww6smiw2ghjh-foo-dummy",
      "name": "foo",
      "out": "/nix/store/fsmr038gnky9bv7z5byyss2hf6iha2q7-foo",
      "outputs": "out dummy",
      "system": "x86_64-linux"
    },
    "inputDrvs": {},
    "inputSrcs": [],
    "name": "foo",
    "outputs": {
      "dummy": {
        "path": "/nix/store/ssv0b9s9w3zw4br6zhghww6smiw2ghjh-foo-dummy"
      },
      "out": {
        "path": "/nix/store/fsmr038gnky9bv7z5byyss2hf6iha2q7-foo"
      }
    },
    "system": "x86_64-linux"
  }
}

In Nix, the derivation function will actually (in addition to producing a store derivation) return an attribute set. And it will add some extra attributes that make it special:

nix-repl> builtins.attrNames foo
[ "all" "builder" "drvAttrs" "drvPath" "dummy" "name" "out" "outPath" "outputName" "outputs" "system" "type" ]

nix-repl> {type = "derivation";}
«derivation ???»

Notably, the derivation function adds outPath attribute, which determines how an attribute set is turned into string:

nix-repl> foo.outPath
"/nix/store/fsmr038gnky9bv7z5byyss2hf6iha2q7-foo"

And also one attribute per output, that will contain a similar attribute set with outPath and outputName attributes changed:

nix-repl> foo == foo.out
true

nix-repl> foo.dummy.outPath
"/nix/store/ssv0b9s9w3zw4br6zhghww6smiw2ghjh-foo-dummy"

nix-repl> foo.dummy.outputName
"dummy"

Copy link
Contributor Author

@gabyx gabyx Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thats why the confusion..., so foo.dummy is a derivation, correct?. So whats the term for it? So dummy can be described as the attribute set of the output dummy of package foo , aka the output derivation :-). but if its not a thing, then lets call it what? derivation output (I am a non-native speaker), so really unsure if thats clearer. Maybe one should just document that pkg.${name} is returned if it exists which is a what?.

I am confused now what type pkgs.${name} really is, if its an attribute set, why does nix repl return <<...>> (e.g. a derivation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pkgs.openssl.bin.bin.bin.bin.bin.bin.bin exists which kind of shows that its self referencing as you stated... kind of confusing....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So pkgs.openssl.bin and pkgs.openssl.man are stored at two different locations, pkgs.openssl.bin == pkgs.openssl.man is false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since very recently Nix now calls this a "package" or "package attribute set": https://nixos.org/manual/nix/stable/glossary.html#package

But I'm not a fan of it because it confuses it with Nixpkgs notion of a package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that while pkgs.openssl.bin.drvPath == pkgs.openssl.doc.drvPath, pkgs.openssl.bin.outPath != pkgs.openssl.doc.outPath.

this is because a single derivation can build multiple outputs.

and if i'm not mistaken, the nix definition of a package is based off of the nixpkgs idea of a package.

@h7x4 h7x4 changed the title lib.attrsets: Correct wrong doumentation in getLib etc lib.attrsets: Correct wrong documentation in getLib etc Feb 10, 2024
@lolbinarycat
Copy link
Contributor

I'm not convinced this is actually a documentation issue.

i'm curious if everything is would still build correctly if lib.attrsets.getOutput returned pkg.outPath instead of just pkg, as it seems like that was the original intent, and it went unnoticed due to string interpolation coercing via outPath anyways.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 1-10 10.rebuild-darwin: 1 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants