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

template: Clean up outputs #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions template/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,39 @@
# Import napalm
inputs.napalm.url = "github:nix-community/napalm";

# Configuring "follows" lets you configure the source for the build environment.
inputs.napalm.inputs.nixpkgs.follows = "nixpkgs";

outputs = { self, nixpkgs, napalm }:
let
# Generate a user-friendly version number.
version = builtins.substring 0 8 self.lastModifiedDate;

# System types to support.
supportedSystems = [ "x86_64-linux" "aarch64-linux" "i686-linux" "x86_64-darwin" "aarch64-darwin" ];

# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = f:
nixpkgs.lib.genAttrs supportedSystems (system: f system);

# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system:
import nixpkgs {
inherit system;
# Add napalm to you overlay's list
overlays = [
self.overlay
napalm.overlay
];
});

forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
in
{
# A Nixpkgs overlay.
overlay = final: prev: {
# Example package
hello-world = final.napalm.buildPackage ./hello-world { };
};
# Advanced: A Nixpkgs overlay.
overlay = final: prev:
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure about this. On one hand this makes the overlay self-contained. On the other, it is no longer pure – and if the overlay no longer works with a single coherent set of packages, there are not many reasons to choose it over just legacyPackages.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I'm not very familiar with both creating and using overlays; I use them very coarsely in my dotfiles when I use them at all. Since the package requires napalm to build, providing it purely would require adding the napalm overlay to prev, right? Is something like this possible?

let
  pkg = import prev {
    overlays = [ napalm.overlay ];
  };
in
  {
    hello-world = pkg.buildPackage ./hello-world { };
  };

Copy link
Member Author

Choose a reason for hiding this comment

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

nix flake check doesn't seem to error on it, so I'll test it out

Copy link
Member Author

Choose a reason for hiding this comment

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

The answer is no, it does not.

Copy link
Contributor

@jtojnar jtojnar Oct 11, 2022

Choose a reason for hiding this comment

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

That would re-create a new Nixpkgs instance from scratch, likely discarding other overlays in the stack.

You could do something like the following:

final: prev:
let
ourOverlay = final: prev:
  {
    hello-world = prev.napalm.buildPackage ./hello-world { };
  };
in
prev.lib.composeExtensions napalm.overlay ourOverlay

but that would also override napalm people put in their overlays.

Or maybe something like:

final: prev:
let
  pkgsWithNapalm = napalm.overlay final prev;
in
  {
    hello-world = pkgsWithNapalm.napalm.buildPackage ./hello-world { };
  };

But that would still not allow people to override napalm.

We could allow that using conditional stacking:

final: prev:
let
  pkgsWithNapalm = if prev ? napalm then prev else napalm.overlay final prev;
in
  {
    hello-world = pkgsWithNapalm.napalm.buildPackage ./hello-world { };
  };

But I think that is too magic and would just befuddle people.

So I tend to use just plain old

final: prev:
  {
    hello-world = prev.napalm.buildPackage ./hello-world { };
  };

And mention the dependency in readme.

But maybe there is some other idiom concerning interdependent overlays that I am not aware of. Maybe ask on Discourse or Matrix?

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the conditional stacking, but yeah, might be complex to have in a template. Not sure that it's common though to have an external builder be passed purely though

Copy link
Member Author

@cyntheticfox cyntheticfox Oct 11, 2022

Choose a reason for hiding this comment

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

Alright, I added an implementation of the conditional stacking I've managed to get working right, alongside some explanatory comments. It's close to what you had, but with napalm.buildPackage as the checked attr, as that's both specifically what we need, and should help avoid collision problems (e.g. there's already a napalm package in nixpkgs, but it's different).

It's probably enough to do things like this, as overlays themselves are more of an advanced use-case.

EDIT: Got it working in 7bfe032 specifically.

let
# To keep the overlay pure, conditionally use the napalm overlay and
# default to using the napalm package from the overlaid nixpkgs if
# it exists.
pkgsWithNapalm = if prev ? napalm.buildPackage then prev else napalm.overlay final prev;
in
{
hello-world = pkgsWithNapalm.napalm.buildPackage ./hello-world { };
};

# Provide your packages for selected system types.
packages = forAllSystems (system: {
inherit (nixpkgsFor.${system}) hello-world;
hello-world = napalm.legacyPackages."${system}".buildPackage ./hello-world { };
});

# The default package for 'nix build'. This makes sense if the
# flake provides only one package or there is a clear "main"
# package.
# flake provides only one package or there is a clear "main"
# package.
defaultPackage =
forAllSystems (system: self.packages.${system}.hello-world);
forAllSystems (system: self.packages."${system}".hello-world);
};
}