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

2.1k Views Asked by At

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
 
1

There are 1 best solutions below

1
On

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