mirror of
https://github.com/bennofs/nix-script
synced 2025-01-10 04:44:21 +01:00
rename to nix-script (nixbang already exists)
This commit is contained in:
parent
12274118ae
commit
45f2a37b67
39
README.md
39
README.md
@ -1,10 +1,15 @@
|
|||||||
nix-bang
|
nix-script
|
||||||
========
|
==========
|
||||||
|
|
||||||
The Nix-bang script allows you to define dependencies at the top of scripts which will then be used to create a nix-shell environment when run. This project is only useful if you're using the nix package manager.
|
Nix-script allows you to define dependencies at the top of scripts which will then be used to create a nix-shell environment when run. This project is only useful if you're using the nix package manager.
|
||||||
|
|
||||||
|
Features:
|
||||||
|
|
||||||
|
* Run scripts in an isolated environment with only the required dependencies
|
||||||
|
* Load scripts in an interpreter in that environment
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
-------------
|
||||||
|
|
||||||
Clone the repository and run:
|
Clone the repository and run:
|
||||||
|
|
||||||
@ -12,24 +17,42 @@ Clone the repository and run:
|
|||||||
$ nix-env -if.
|
$ nix-env -if.
|
||||||
```
|
```
|
||||||
|
|
||||||
This will install the `nix-bang` and `nix-bangi` files into your user profile.
|
This will install the `nix-script` and `nix-scripti` files into your user profile.
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
To use `nix-bang`, you need to add a header to your file. Here is an example for a haskell file:
|
To use `nix-script`, you need to add a header to your file. Here is an example for a haskell file `example.hs`:
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
#!nix-bang
|
#!nix-script
|
||||||
#!> haskell
|
#!> haskell
|
||||||
#! haskell | text lens optparse-applicative
|
#! haskell | text lens optparse-applicative
|
||||||
#! shell | nix nix-prefetch-scripts
|
#! shell | nix nix-prefetch-scripts
|
||||||
|
|
||||||
|
{- ... code using text, lens and optparse-applicative -}
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = {- ... -}
|
||||||
```
|
```
|
||||||
|
|
||||||
The first line just tells the shell to use `nix-bang` when executing the script. The next line is then read by `nix-bang` to determine the language used for running the script. In this case, we tell `nix-bang` that we want haskell, so it will use `runhaskell` to execute the script.
|
The first line just tells the shell to use `nix-script` when executing the script. The next line is then read by `nix-script` to determine the language used for running the script. In this case, we tell `nix-script` that we want haskell, so it will use `runhaskell` to execute the script.
|
||||||
|
|
||||||
The next lines the specify dependencies of the script. The first entry on each line is the language of the following dependencies. This is required so that language-specific names can be converted to the correct nix attribute names. You should have one line per language. In our case, we say that we want to use the `text`, `lens` and `optparse-applicative` haskell packages. We also want that `nix` and `nix-prefetch-scripts` are available in $PATH (the `shell` language doesn't apply any renaming to their dependencies and just passes them through unmodified).
|
The next lines the specify dependencies of the script. The first entry on each line is the language of the following dependencies. This is required so that language-specific names can be converted to the correct nix attribute names. You should have one line per language. In our case, we say that we want to use the `text`, `lens` and `optparse-applicative` haskell packages. We also want that `nix` and `nix-prefetch-scripts` are available in $PATH (the `shell` language doesn't apply any renaming to their dependencies and just passes them through unmodified).
|
||||||
|
|
||||||
|
We can now mark the script executable and run it:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ chmod +x ./example.hs
|
||||||
|
$ ./example.hs # This works even if you don't have ghc, text, lens or optparse-applicative installed
|
||||||
|
```
|
||||||
|
|
||||||
|
We can also load the script in `ghci`:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nix-scripti ./example.hs
|
||||||
|
```
|
||||||
|
|
||||||
Contributing
|
Contributing
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
12
default.nix
12
default.nix
@ -1,17 +1,17 @@
|
|||||||
{ pkgs ? import <nixpkgs> {} }:
|
{ pkgs ? import <nixpkgs> {} }:
|
||||||
|
|
||||||
pkgs.stdenv.mkDerivation {
|
pkgs.stdenv.mkDerivation {
|
||||||
name = "nix-bang";
|
name = "nix-script";
|
||||||
src = ./nix-bang.hs;
|
src = ./nix-script.hs;
|
||||||
phases = [ "buildPhase" "installPhase" "fixupPhase" ];
|
phases = [ "buildPhase" "installPhase" "fixupPhase" ];
|
||||||
buildPhase = ''mkdir $out; ghc -O2 $src -o $out/nix-bang -odir $TMP'';
|
buildPhase = ''mkdir -p $out/bin; ghc -O2 $src -o $out/bin/nix-script -odir $TMP'';
|
||||||
installPhase = ''ln -s $out/nix-bang $out/nix-bangi'';
|
installPhase = ''ln -s $out/bin/nix-script $out/bin/nix-scripti'';
|
||||||
buildInputs = [ (pkgs.haskellPackages.ghcWithPackages (hs: with hs; [lens text])) ];
|
buildInputs = [ (pkgs.haskellPackages.ghcWithPackages (hs: with hs; [lens text])) ];
|
||||||
meta = {
|
meta = {
|
||||||
homepage = https://github.com/bennofs/nix-bang;
|
homepage = https://github.com/bennofs/nix-script;
|
||||||
description = "A shebang for running inside nix-shell.";
|
description = "A shebang for running inside nix-shell.";
|
||||||
license = pkgs.lib.licenses.bsd3;
|
license = pkgs.lib.licenses.bsd3;
|
||||||
maintainers = [ pkgs.lib.maintainers.bennofs ];
|
maintainers = [ pkgs.lib.maintainers.bennofs ];
|
||||||
platforms = pkgs.haskellPackages.ghc.meta.platforms;
|
platforms = pkgs.haskellPackages.ghc.meta.platforms;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
0
nix-bang.hs → nix-script.hs
Executable file → Normal file
0
nix-bang.hs → nix-script.hs
Executable file → Normal file
Loading…
Reference in New Issue
Block a user