Need help installing custom script (from a flake) into my config AND in a nix shell

Hello, I’m new to nix, it’s been a one-month journey so far, but I love it already. Recently, I created my first flake; it’s almost identical to the “overengineered hello” template from nix flake init. The script is a maintenance script to help with recurring tasks; also it’s not so useful, it makes me practice, please don’t judge.

Anyway, I need that script installed as part of my configuration for everyday usage, but also part of a nix shell I plan to use for bootstrapping.

For the nix shell, I have absolutely no idea how to install it, as I can’t find a way to add another flake than the nixpkgs one. So that’s the first thing I’d like to find out.

The second thing is about the “regular” install, I found more resources discussing that, but I cannot make it work. I added the repo as a flake:

{
  description = "Nindouja's flake";

  outputs = { ...  }:  {
    ...
  };

  inputs = {
    ...
    ninja-script = {
      url = "git+https://gitlab.vba.ovh/snippets/3.git";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
}

And to install it like so in home manager (also tried in system config):

{pkgs, ...}: {
  home.packages = with pkgs; [
    #ninja-script.defaultPackage
  ];
}

In both cases I got this error and I don’t know how to interpret it.

error: attribute 'override' missing

       at /nix/store/g861759ghxxwvyfdbv17xf3iahgm8rcb-source/pkgs/stdenv/darwin/default.nix:461:15:

          460|
          461|       ninja = super.ninja.override { buildDocs = false; };
             |               ^
          462|

Any help would be greatly appreciated.

I managed to install it !! For future references, I used this syntax in my home/system packages:
ninja.packages.${system}.ninja
(I renamed it from ninja-script to ninja but that’s not relevant; the important part is the ${system} part

I’m gonna start looking to add it to my shell now I have more info. But I still have no idea how to add my flake as an input.

By “nix shell” do you mean a development shell? You would add it the same way you did for home or system packages – add it to nativeBuildInputs in flake.nix, like so:

    {
      devShells.${system}.default = pkgs.mkShell {
        nativeBuildInputs = [ <reference to your custom script> ]; # <- this
      };
    };

In the code from the original post it’s already added to the inputs:

Are you referring to something else as an input, like an override of a package?

When I run nix-shell, nix will load the shell.nix file in the current directoy; but does it execute it in the “context” of the flake.nix in that same directory ? I made the assumption shell.nix was it’s own clustered context ?

That does not seem to be the case, because with that shell.nix file in the same directory as the flake:

{pkgs ? import <nixpkgs> {}}:
with pkgs;
  pkgs.mkShell {
    packages = [
      # Nix
      nix
      home-manager

      # Secrets management
      age
      sops
      ssh-to-age

      # Ninja dependencies
      git
      alejandra
      deadnix

      # Utilities
      vim
      wget
      jq
      yq
      unzip
      nix-output-monitor # TODO mke it work

   
      ninja.packages.${system}.ninja
    ];
}

Nix cannot find my package. That’s not surprising to me as I have done nothing to make shell.nix aware of my package. For home/system config I inherit it when building my output.

In case it’s not clear, the package I’m providing via flake is in a different repo than my actual config; both have a flake.nix file
The package repo: Ninja ($3) · Snippets · GitLab
The config repo: Nindouja / NixOs · GitLab

Correct, shell.nix is not flake-aware by itself. devShells is its analog in flakes.

Technically you can use builtins.getFlake to extract things from flakes in .nix files:

# flake.nix
{
  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
  };
}
# shell.nix
{
  pkgs ? import <nixpkgs> { },
}:
with pkgs; # NOTE: With scopes have gotchas. See https://nix.dev/guides/best-practices.html#with-scopes
pkgs.mkShell {
  packages = [
    age # Some sample package from pkgs
    (builtins.getFlake "path:////tmp/tmp.4cGlth4cgi").packages.${system}.default # this tmp dir contains flake.nix and shell.nix files
  ];
}

Which results in:

❯ which hello
which: no hello in ...
❯ nix-shell ./shell.nix
❯ which hello
/nix/store/...

But this approach takes <nixpkgs> reference from channels. If you are using flakes anyway – you probably want devshell if you want to pull in development-only packages.

Perfectly clear !
I saw some mentions of dev shells during my learning but as I’m new I was going one step at a time and wanted to understood nix shells better first. I guess I’ll look into them right now. Thanks a lot !!

I hate when links in frul points to 404 repo, so here is the new link to the flake/script I mentioned above. It was just a snippet but I moved it to an actual repo.
Sorry for the ping, I could not edit my post it seems