strickinato
strickinato

Reputation: 1

On nixos - successfully building a package with nix-build, but unable to import it into my configuration.nix

On nixos. My goal is to run a deno script in a regular interval.

To do so, I have a derivation for the script on my my machine, and I want to use it in my configuration.nix, and include it in a systemd service.

The Working Derivation:

# path/to/my-package/default.nix

{ pkgs ? import <nixpkgs> {} }:

with pkgs;

let
  source = stdenv.mkDerivation {
    name = "main-file";
    src = builtins.path { name = "main-file-dir"; path = ./.; };
    installPhase = ''
      mkdir -p $out
      cp main.ts $out/
    '';
  };
in
let
  myScript = pkgs.writeShellScriptBin "myscript" ''
    ${pkgs.deno}/bin/deno run ${source}/main.ts
  '';
in
stdenv.mkDerivation rec {
  name = "my-package";

  src = builtins.path { name = "my-package-src"; path = ./.; };

  buildInputs = [ source myScript pkgs.deno ];

  # this is kinda not doing anything, but it fails without it
  installPhase = ''
    mkdir -p $out/bin
  '';
}

Running nix-build succeeds without a problem. Running nix-shell default.nix pops me into a shell, and I can happily run myscript and the script runs. This is all well and good!

The problem

However, when I go to import it. and run sudo nixos-rebuild switch. It throws an error! Here's what that looks like:

# configuration.nix

{ config, pkgs, ... }:

let
  myPackage = import path/to/my-package/default.nix { inherit pkgs; };
in
 ...
 services.systemd.my-service = {
  ... #use `myPackage` here.
 }
 ...

And the error thrown is: attribute 'deno' missing, at path/to/my-package/default.nix:17:7

The question:

Any ideas?

Some paths I've walked

I've tried a number of different ways of importing it - all which lead to basically the same result:

import path/to/my-package/default.nix { inherit pkgs; }; callPackage path/to/my-package/default.nix { };

Upvotes: 0

Views: 1219

Answers (1)

Robert Hensing
Robert Hensing

Reputation: 7359

It seems that you have a different channel configuration for root and for your user, as can be seen from the difference between

nix-instantiate '<nixpkgs>' --eval --expr

and

sudo nix-instantiate '<nixpkgs>' --eval --expr?

I've stopped using channels and I'll switch from my custom solution to flakes soon. If you want to keep using channels, you could configure either user's NIX_PATH to point to the other channel, and always use that user to update the channel.

I can't reproduce your situation in a test VM, which reads:

[alice@machine:~]$ echo $NIX_PATH
nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels

I wish I had a more specific recommendation for nix-channel, but the truth of the matter is that the community is moving away from it. Before flakes, some people used their own custom setup. With flakes, a setup without nix-channel or even NIX_PATH is within reach.

Beware of the "nix registry" though. It is a lot like channels, but unlike channels, you can actually avoid it easily.

Upvotes: 1

Related Questions