3

I'm trying to use an external (as in not present in nixpkgs) flake in my home-manager (using nix-darwin) config.

The flake has a default output which is the binary produced by buildGoModule: https://github.com/pcasaretto/dotenv/blob/f41d74aa56c2528c46f58977010c6ce99619921a/flake.nix

          default = buildGoModule {
            pname = "dotenv";
            inherit version;
            # In 'nix develop', we don't need a copy of the source tree
            # in the Nix store.
            src = ./.;

            vendorSha256 = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo=";
          };

I've managed to add the flake to my config, it builds but the binary does not show up in my path

https://github.com/pcasaretto/nix-home/commit/18e82337efdb0579588e6633c0ae8006788ae402

# flake.nix
dotenv.url = "github:pcasaretto/dotenv";
dotenv.inputs.nixpkgs.follows = "nixpkgs-unstable";
# ...
home-manager.extraSpecialArgs = { dotenv = dotenv; };
# home.nix
{ config, pkgs, lib, dotenv, ... }:
# ...
home.packages = [
# ...
dotenv
Peter Becich
  • 989
  • 3
  • 14
  • 30
Paulo Casaretto
  • 967
  • 10
  • 33
  • 1
    Please [edit] to include enough information to allow answers _in the question itself_, as per [mre] guidelines. The goal is for answers to remain useful to other people even if links break (your github repo is deleted or renamed, f/e); and to help search engines index appropriately. Treating links as purely supplemental information that _aren't_ allowed to be referenced in determining whether an answer is correct helps ensure we're meeting that goal. – Charles Duffy Sep 20 '22 at 13:59
  • I will note at a high level that `builtins.getFlake` is your friend, and once you've called that you can refer to `packages.${builtins.currentSystem}`, but can't really say more until I've seen the code (indeed, for all I can tell from the question text so far, you may well already know the above). – Charles Duffy Sep 20 '22 at 13:59
  • ...that's elided a bit too much -- in an ideal world it would be the shortest thing _that can be run without changes_ to see the same problem or test proposed fixes, and the code is cut down enough that it's definitely no longer valid syntax. That said, the `dotenv` flake itself isn't what you need in your package list; instead, you need something like `dotenv.packages.${builtins.currentSystem}.default`. Personally, I name my flakes something like `dotenvFlake` in the inputs section to distinguish the dotenv _flake_ from the dotenv _package_, but YMMV. – Charles Duffy Sep 20 '22 at 14:06
  • While this last bit is purely personal taste, another practice I'm fond of is `packages = rec { default = dotenv; dotenv = buildGoModule { ...; }; }` -- that way someone can `inherit (dotenvFlake.packages.${builtins.currentSystem}) dotenv;` to pull `dotenv` into an attrset. – Charles Duffy Sep 20 '22 at 14:10
  • BTW, using `builtins.getFlake` inside `nix repl` is a good way to get a feel for all this, since it lets you interactively explore where inside the flake everything you've defined lives. – Charles Duffy Sep 20 '22 at 14:12
  • 1
    (the `packages.${system}.default` location is assuming that `nix flake check` passes for your flake, thus that it's generally well-formed... it is, right?) – Charles Duffy Sep 20 '22 at 14:17
  • It is. `nix flake check` passes and `nix build` outputs the binary correctly. – Paulo Casaretto Sep 20 '22 at 14:22
  • 1
    To test this out, I've changed to `dotenv.packages.aarch64-darwin.default` the line included under `home.packages` and it worked. That made sense. – Paulo Casaretto Sep 20 '22 at 14:23
  • Great; added an answer to that effect. – Charles Duffy Sep 20 '22 at 15:14

1 Answers1

4

home.packages needs to include the package from the flake, not the flake itself.

Change:

home.packages = [ # ...
  dotenv
];

to:

home.packages = [ # ...
  dotenv.packages.${pkgs.system}.default
];

If you aren't in a flake (or other purity-enforced) context, you can use builtins.currentSystem instead of pkgs.system.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Is there an opportunity here for defining something like a `getFlakeDefaultPackage` function that streamlines this? – Paulo Casaretto Sep 20 '22 at 16:31
  • 1
    For completeness, this was the solution I ended up with: https://github.com/pcasaretto/nix-home/commit/27bb01e084c0f550d9fba5e636c62225f1452732 – Paulo Casaretto Sep 20 '22 at 16:32