How to export flake.nix as a package?

Hi there, I’m trying to add a custom package to my home-manager configuration but have been stuck with two problems with the following flake.nix.

{
  description = "Leet your code in command-line.";

  inputs.nixpkgs.url      = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.rust-overlay.url = "github:oxalica/rust-overlay";
  inputs.flake-utils.url  = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay ]; };

        platform = pkgs.makeRustPlatform {
          rustc = rust-overlay.rust-bin.nightly.latest.minimal;
          cargo = rust-overlay.rust-bin.nightly.latest.minimal;
        };
      in 
      with pkgs;
      {
        defaultPackage.${system} = platform.buildRustPackage rec {
          pname = "leetcode-cli";
          version = "0.3.9";

          src = fetchCrate {
            inherit pname version;
            sha256 = "1aiksg4iyrhkmwl4djy7bm3xjras4qqfixi6ml8k6pz0s9ynknws";
          };

          cargoSha256 = "1ldzr2bspy288nydk2lnkc4akc2myk2jk7aw87vr4kmzl0jmh329";

          # a nightly compiler is required unless we use this cheat code.
          RUSTC_BOOTSTRAP = 0;

          # CFG_RELEASE = "${rustPlatform.rust.rustc.version}-nightly";
          CFG_RELEASE_CHANNEL = "ngihtly";

          nativeBuildInputs = [
            pkg-config
            rust-bin.nightly."2021-05-16".minimal
          ];

          buildInputs = [
            openssl
            dbus
            sqlite
          ];

          meta = with lib; {
            description = "Leet your code in command-line.";
            homepage = "https://github.com/clearloop/leetcode-cli";
            licenses = licenses.mit;
            maintainers = with maintainers; [ congee ];
            mainProgram = "leetcode";
          };
        };
      }
    );
}

The first issue is an error when I try to nix build .

error: flake output attribute ‘defaultPackage.x86_64-linux’ is not a derivation

How can I get a derivation from nixpkgs.platform.buildRustPackage?


The second issue is I cannot just add this flake.nix to home.packages

home.packages = [ import ./packages/leetcode-cli/flake.nix ];
# nor can I add it as an overlay
nixpkgs.overlays = [
  (self: super: { leetcode-cli = import ../packages/leetcode-cli/default.nix; })
];

I have already searched on the internet, but, unfortunately, could not find any relevant code for help. :cry:

Is there any pointers my fellow nix gurus?

I got a video on using flakes in out-of-nixpkgs-tree code. Probably would answer a lot of your questions: Nix - Flakes for out-of-tree code - YouTube

5 Likes

Thanks, I just watched that video again lol. After some trial & error, I found a small fix that makes nix build pass. :grinning:

- pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay ]; };
+ pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay.overlay ]; };

The second issue of being unable to find a way to use this flake.nix as a home-manager package still persists :sweat_smile:

1 Like

The second problem is solved by setting an overlay field in this flake.nix, then pass this overlay to home-manager.

package = platform.buildRustPackage { ... };  # ommited
outputs = { self, ...}: {
  defaultPacakge = package;
  overlay = final: prev: { leetcode = package; };
}

I was inspired by the usage of specialArgs from Accessing flakes from inside home-manager modules · Issue #1698 · nix-community/home-manager · GitHub though I dont’ understand and did not use it :sweat_smile:

2 Likes

Early replies to that issue suggest it doesn’t, but extraSpecialArgs does work now for Home Manager. I use it in my configs to make the contents of my flake’s packages attribute available to Home Manager as flakePkgs.

1 Like
Hosted by Flying Circus.