Skip to content

[remove nixpkgs] part 2: generate flake.nix using builtins.fetchClosure #1209

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

Merged
merged 11 commits into from
Jun 29, 2023

Conversation

savil
Copy link
Collaborator

@savil savil commented Jun 23, 2023

Summary

See #1208 for motivation.
That previous PR handled the "writing the system info to the lockfile". In this PR:

  1. Generate the flake.nix from flake_remove_nixpkgs.nix.tmpl template.
    • this new template uses builtins.fetchClosure to download the package's binary directly from the binary-cache-store (like cache.nixos.org). This lets us bypass nixpkgs for nix packages in the binary store.
  2. The template data is shellgen.flakePlan.
  3. In the flakePlan, we:
    a. filter flakeInputs to be those not in the binary store (like remote or local flakes)
    b. add a new field Packages []*nix.Package.

In nix.Package, we have a few new methods specific to the BinaryStore, like IsInBinaryStore and PathInBinaryStore.

Testing note:
I was stymied in extensively testing this by:

  • search index lacking x86_64-darwin system info (and I have an intel mac)
  • devbox cloud having limitations (will share details elsewhere)
  • the simple test plan below took me many hours to complete.

cc @mikeland73

How was it tested?

Tested with examples/development/go/hello-world.

For a more complex example, with a flake, the examples/flakes/remote produces this flake.nix:

{
   description = "A devbox shell";

   inputs = {
     nixpkgs.url = "http://lax.devbox-nixed.internal/nixos/nixpkgs/archive/f80ac848e3d6f0c12c52758c0f25c10c97ca3b62.tar.gz";
     gh-nixos-nixpkgs-5233fd2ba76a3accb5aaa999c00509a11fd0793c.url = "github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c";
     gh-F1bonacc1-process-compose.url = "github:F1bonacc1/process-compose";
   };

   outputs = {
     self,
     nixpkgs,
     gh-nixos-nixpkgs-5233fd2ba76a3accb5aaa999c00509a11fd0793c,
     gh-F1bonacc1-process-compose,
   }:
       let
          pkgs = nixpkgs.legacyPackages.x86_64-linux;
       in
       {
         devShells.x86_64-linux.default = pkgs.mkShell {
           buildInputs = [
             gh-nixos-nixpkgs-5233fd2ba76a3accb5aaa999c00509a11fd0793c.legacyPackages.x86_64-linux.hello
             gh-nixos-nixpkgs-5233fd2ba76a3accb5aaa999c00509a11fd0793c.legacyPackages.x86_64-linux.cowsay
             gh-F1bonacc1-process-compose.packages.x86_64-linux.process-compose
           ];
         };
       };
 }

Copy link
Collaborator Author

savil commented Jun 23, 2023

@savil savil force-pushed the savil/rm-nixpkgs-2-flake-dot-nix branch 4 times, most recently from eed5ad6 to 6f26857 Compare June 23, 2023 13:22
Base automatically changed from savil/rm-nixpkgs-1-lockfile to main June 23, 2023 14:30
@savil savil force-pushed the savil/rm-nixpkgs-2-flake-dot-nix branch 5 times, most recently from ecbc74f to 163ddb4 Compare June 23, 2023 23:01
@savil savil requested review from gcurtis and loreto June 23, 2023 23:22
@savil savil marked this pull request as ready for review June 23, 2023 23:22
@savil savil requested a review from mikeland73 June 23, 2023 23:35
@gcurtis
Copy link
Collaborator

gcurtis commented Jun 28, 2023

Sorry for the delay - I just remembered that I never reviewed this. I'll take a look tomorrow morning.

@savil savil force-pushed the savil/rm-nixpkgs-2-flake-dot-nix branch from 163ddb4 to 3df3c5c Compare June 28, 2023 13:41
Copy link
Collaborator

@gcurtis gcurtis left a comment

Choose a reason for hiding this comment

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

I had some comments, but approving to keep the train moving.

allInputs = lo.Filter(allInputs, func(item *nix.Package, _ int) bool {
// This is temporary, so that we can support the existing flake.nix.tmpl
// as well as the new flake_remove_nixpkgs.nix.tmpl.
return !featureflag.RemoveNixpkgs.Enabled() || item.SystemInfo() == nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Won't this just remove everything when the feature is enabled? In which case allInputs could just be nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will continue to include packages (like local or remote flakes) that don't have system info

Name: attributePath,
FromStore: nixosCacheURL,
FromPath: filepath.Join("/nix/store", storeDir),
// TODO add ToPath:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you looked at the builtins.storePath function? If we're running in impure mode, then it might make things a little easier. It doesn't require a content-addressed hash and it will automatically use the Nix-configured binary caches (aka substituters). That'll make this work automatically with nixed in Devbox Cloud.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't yet. Thanks for the pointer. Will look into it.

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

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

Not blocking, but I would really want to avoid adding a new Package struct. I also think system info should be part of resolve. Ideally system info can be abstracted away so that the whatever generates the flake can just get the data if it's present.

btw, you should assume that we will not be able to avoid nixpkgs for certain packages for the short and medium term, so we need to structure this in a clean way where both types can coexist.

@savil savil force-pushed the savil/rm-nixpkgs-2-flake-dot-nix branch from 3df3c5c to 755eff3 Compare June 28, 2023 22:48
@savil
Copy link
Collaborator Author

savil commented Jun 28, 2023

@gcurtis addressed comments and made updates

@mikeland73 Folded flakePackages into nix.Package. Addressed the concern around calling lockfile.Resolve in the getter for FromPath in nix.Package.

@savil savil requested review from mikeland73 and gcurtis June 29, 2023 00:46
Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

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

This is great! I really like how this ended up.

Comment on lines 77 to 84
pkg := &Package{
URL: *pkgURL,
lockfile: locker,
Raw: raw,
normalizedPackageAttributePathCache: "",
}

return pkg
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, you can revert this if you want. You can also omit normalizedPackageAttributePathCache if you used name fields.

Comment on lines 422 to 424
func (p *Package) IsInBinaryStore() bool {
return p.isVersioned()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a better way to determine this? For example only true if lock file data has system info?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤔 yeah, we could add that check too. In my previous iteraiton of this PR, I was checking both p.isVersioned() and "has system info".

I can add that here. It feels weird to possibly make network-calls in an if-statement but I understand the architecture better now so lets roll with it.

var err error
shellPlan.FlakeInputs, err = flakeInputs(ctx, devbox)
userPackages := devbox.PackagesAsInputs()
pluginPackages, err := devbox.PluginManager().PluginInputs(userPackages)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll rename PluginInputs and PackagesAsInputs in a follow up.

Also, I'm thinking of changing plugins to encapsulate it more. Having plugins show up in shellgen package is not idael

@savil savil merged commit 18bbfe9 into main Jun 29, 2023
@savil savil deleted the savil/rm-nixpkgs-2-flake-dot-nix branch June 29, 2023 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants