In a flake.nix how can I run a script from it and use the output in the same flake.nix - nix

This is the sequel of this question.
I've a bash list of command that generated a file nix directory when these commands are executed.
mkdir nix
rm -fr node_module
node2nix -16 --development --input package.json --lock package-lock.json --node-env ./nix/node-env.nix --composition ./nix/default.nix --output ./nix/node-package.nix
I have flake.nix file that use nix to create an envirnoment.
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
#npm_pack = import (./nix );
npm_pack = import ./nix { inherit pkgs ;};
in with pkgs;{
#devShell = mkShell { buildInputs = [ npm_pack.package ];};
devShell = npm_pack.shell;
});
}
It is executed with this command:
nix develop --extra-experimental-features nix-command --extra-experimental-features flakes --ignore-environment
Is there a way to modify the flake.nix file to create the nix directory and then do the work it has to do with nix directory.
I know that I could ONE create bash file (see the in answer why I don't like it)
In order to create the flake.nix I thinking about using something like a sheelhook in the beginning.I need too to be sure that node and node2nix are installed. Therefore I need those line
node2nix.url ="github:svanderburg/node2nix"; # in the input
nodejs = pkgs.nodejs-16_x; #in the output

running this script works
#/nix/store/4xw8n979xpivdc46a9ndcvyhwgif00hz-bash-5.1-p16/bin/bash
#nix-shell -p node2nix nodejs stdenv --pure
npm init -y
npm install node-gyp-build
mkdir nix;
rm -fr node_modules ;
node2nix -16 --development --input package.json --lock package-lock.json --node-env ./nix/node-env.nix --composition ./nix/default.nix --output ./nix/node-package.nix
nix develop --extra-experimental-features nix-command --extra-experimental-features flakes --ignore-environment
but it doesn't work if somebody has not the bash in the directory than my bash.
Therefore I prefer a solution consisting of implementing a big flake.nix because I'm sure it works if nix is installed
and it need to files : the flake.nix and the script.sh

Related

How to refer to custom derivation in shell.nix?

I'm very new to Nix. I'd like to refer to project scripts in my shell.nix file, so that when I cd into my project directory I can refer to them by name, and I can keep them up-to-date whenever the sources change.
To learn how to do this, I created a very simple derivation for a shell script. Eventually I'd like to use other languages, but I'm starting simple. It looks like this:
project
nix
myScript
default.nix
builder.sh
shell.nix
# default.nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation rec {
name = "myScript";
echo = pkgs.coreutils + "/bin/echo";
builder = "${pkgs.coreutils}/bin/bash";
args = [ ./builder.sh ];
}
# builder.sh
$echo "$echo Hello world" > $out
When I run nix-build myScript.nix it creates a symlinked result file that looks like this:
/nix/store/3mfkgajns47hfv0diihzi2scwl4hm2fl-coreutils-9.1/bin/echo Hello world
I tried referencing this in my shell.nix file like this:
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/bf972dc380f36a3bf83db052380e55f0eaa7dcb6.tar.gz") {} }:
let
myScript = import ./myScript {};
in
pkgs.mkShell {
buildInputs = [
myScript
];
shellHook = ''
echo Loading shell.nix
'';
}
But whenever I enter the projects directory and run the command `myScript, I get an error:
zsh: command not found: myScript
I already have direnv configured correctly, which I can confirm by adding other shell tools and checking their versions. So it's something wrong with my nix files.
I'm almost certainly doing something wrong here. I know I can simplify this with pkgs.writeShellScriptBin, but the shell script is more a minimal example of what I want to get working. Eventually I'd use more complex derivations.
What I think is wrong
I think the myScript derivation or builder is doing something wrong. It does create the expected output file (i.e. I can chmod +x and run it, and it works) but I suspect I need to tell nix how to run it? I'm not sure. And also I might be importing the derivation incorrectly.
This is a problem with your default.nix, not your shell.nix.
For mkShell to work with buildInputs as you intend, you need $out to be a directory with an $out/bin/myScript, not a file on its own. nixpkgs has a helper that will do this for you, in https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix --
# default.nix; no builder.sh needed
{ pkgs ? import <nixpkgs> {} }:
pkgs.writeShellScript "myScript" ''
echo "Hello world" # use the bash-builtin echo, not the external coreutils one
'';

Failed when `nix-build hello.nix`

I followed the steps on http://lethalman.blogspot.com/2014/08/nix-pill-8-generic-builders.html to build GNU Hello, and here is the files I used to build GNU hello 2.9:
$ wget -c http://ftp.gnu.org/gnu/hello/hello-2.9.tar.gz
hello.nix:
$ cat hello.nix
let
pkgs = import <nixpkgs> {};
mkDerivation = import ./autotools.nix pkgs;
in mkDerivation {
name = "hello";
src = ./hello-2.9.tar.gz;
}
autotools.nix:
$ cat autotools.nix
pkgs: attrs:
with pkgs;
let defaultAttrs = {
builder = "${bash}/bin/bash";
args = [ ./builder.sh ];
baseInputs = [ gnutar gzip gnumake gcc binutils coreutils gawk gnused gnugrep ];
buildInputs = [];
system = builtins.currentSystem;
};
in
derivation (defaultAttrs // attrs)
builder.sh:
$ cat builder.sh
set -e
unset PATH
for p in $buildInputs; do
export PATH=$p/bin${PATH:+:}$PATH
done
tar -xf $src
for d in *; do
if [ -d "$d" ]; then
cd "$d"
break
fi
done
./configure --prefix=$out
make
make install
Error messages:
$ nix-build hello.nix
these derivations will be built:
/nix/store/d84l57agx3rmw00lxs8gjlw8srmx1bh9-hello.drv
building '/nix/store/d84l57agx3rmw00lxs8gjlw8srmx1bh9-hello.drv'...
/nix/store/vv3xqdggviqqbvym25jf2pwv575y9j1r-builder.sh: line 7: tar: No such file or directory
builder for '/nix/store/d84l57agx3rmw00lxs8gjlw8srmx1bh9-hello.drv' failed with exit code 127
error: build of '/nix/store/d84l57agx3rmw00lxs8gjlw8srmx1bh9-hello.drv' failed
It seems there is gnutar in the autotools.nix but builder still complains tar: No such file or directory, why is this?
The problem is probably that gnutar is in the baseInputs list, while the buildInputs list you are building your PATH from is totally empty so nothing will be on your PATH. Try changing the for line in your shell script so that it uses the concatentation of both lists to build the path:
for p in $baseInputs $buildInputs; do
You can add echo $PATH to your builder script to debug issues like this.
That is what the blog post author was asking you to do in this sentence from the post:
Complete the new builder.sh by adding $baseInputs in the for loop together with $buildInputs.

node2nix override; wrapProgram: command not found

I'm packaging a node script with an external dependency (GraphicsMagick), and when attempting to override the derivation generated from node2nix I get the error:
wrapProgram: command not found
The following text goes into detail of what I've tried to solve this error.
Reproducing the problem from scratch
I've created a minimal git repository that reproduces this problem if you'd just like to take a look there. Else, the steps to reproduce the problem are below.
Initial Shell Session:
In an empty directory, run:
npm init -y
npm install --save gm
curl https://i.imgur.com/addSfQi.jpg > image.png
(npm version: 5.6.0 & node version v8.9.4)
Create index.js
#!/usr/bin/env node
const path = require("path"); // node.js builtin
const gm = require("gm"); // GraphicsMagick module
const imagePath = path.join(__dirname, "image.png");
// Flip image horizontally and write to disk
gm(imagePath)
.flop()
.write(imagePath, error => {
console.log("error:", error);
});
Add a "bin" section to package.json:
"bin": "index.js"
Generate *.nix files with node2nix
node2nix -8 -l package-lock.json
Create override.nix
{ pkgs ? import <nixpkgs> {}
, system ? builtins.currentSystem
}:
let
nodePackages = import ./default.nix {
inherit pkgs system;
};
in
nodePackages // {
package = nodePackages.package.override (oldAttrs: {
postInstall = ''
wrapProgram "$out/bin/test-nodejs-gm-nixpkg" --prefix PATH : "${pkgs.graphicsmagick}/bin"
'';
});
}
Build nix package
nix-build override.nix -A package
The above fails with:
/nix/store/*/setup: line 95: wrapProgram: command not found
Helpful Resources
node2nix git repository - includes some basic examples.
example override in nixpkgs - example of how nixpkgs uses wrapProgram in postInstall with files generated by node2nix.
wrapProgram is contained within the makeWrapper package.
nativeBuildInputs = oldAttrs.nativeBuildInputs or [] ++ [ pkgs.makeWrapper ];
As mentioned by #ppb in the comments.

How to package a command-line tool in Nix?

Suppose you have a simple collection of bash scripts making up a command line tool, with a primary script in bin/ and some library scripts in lib/, all to be packaged with Nix using tool.nix with a default.nix for convenience:
scriptdir
└─ bin/
└─ tool
└─ lib/
└─ default.nix
└─ tool.nix
What should tool.nix look like in order to correctly package this tool, allowing to execute tool in the shell with tool <args>?
After some help from IRC, the following tool.nix works:
{ stdenv }:
let
version = "0.0.1";
in stdenv.mkDerivation
rec
{
name = "tool-${version}";
src = ./.;
installPhase =
''
mkdir -p $out
cp -R ./bin $out/bin
cp -R ./lib $out/lib
'';
}
For completeness, default.nix would look like
{ pkgs ? import <nixpkgs> {} }:
pkgs.callPackage ./tool.nix {}
and can be installed by calling nix-env -f ./default.nix -i from scriptdir.

Override scripts in nix derivations

Is there a way to override scripts and/or configs in an existing derivation without having to recompile the whole package?
I'd like to create a new version of gnome-session with modified $out/share/gnome-session/sessions/gnome.session modified. Using overridePackage I can change the preFixup phase, but this causes the whole gnome-session package to be recompiled.
A simple solution to problem is creating a new derivation without any sources. The trick is to create links to every file and directory in the base derivation, except for any file(s) in need of modification. These files are handled explicitly according to needs.
The snippet below shows how to create a new gnome-session with xmonad instead of gnome-shell.
{ nixpkgs ? import <nixpkgs> {} }:
let
inherit (nixpkgs) pkgs;
in
pkgs.stdenv.mkDerivation {
name = "gnome-session";
builder = pkgs.writeText "builder.sh" ''
# source standard environment
. $stdenv/setup
# shorthands
refpkg=${pkgs.gnome3.gnome_session}
file=share/gnome-session/sessions/gnome.session
# create output dirs for new derivation
mkdir -p $out/share
mkdir -p $out/share/gnome-session/sessions
# link unchanged files from the original gnome-session
ln -sf $refpkg/bin $out
ln -sf $refpkg/libexec $out
find $refpkg/share -maxdepth 1 \
-not -name gnome-session -exec ln -sf {} $out/share \;
# change gnome-shell to xmonad
sed 's/org.gnome.Shell/xmonad/' $refpkg/$file > $out/$file
'';
# make sure gnome-session is installed before deriving it!
buildInputs = [ pkgs.gnome3.gnome_session ];
}

Resources