diff --git a/README.md b/README.md index f8d502e..cc3a37b 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,25 @@ We can also load the script in `ghci`: $ nix-scripti ./example.hs ``` +or open a nix-shell with the requested packages: + +``` +$ nix-scripts ./example.hs +``` + +### Supported languges + +| Identifiers | Language | +|:--------------------:|:---------------------| +| `haskell` | Haskell | +| `python2`, `python3` | Python | +| `perl` | Perl 5 | +| `javascript` | JavaScript (node.js) | +| `shell` | Shell script (bash) | + +Anything else will be treated as "passthough", meaning the rest of +the line will be intpreted as nixpkgs attributes as-is. + Contributing ------------ diff --git a/default.nix b/default.nix index 6af4803..1c95966 100644 --- a/default.nix +++ b/default.nix @@ -5,7 +5,10 @@ pkgs.stdenv.mkDerivation { src = ./nix-script.hs; phases = [ "buildPhase" "installPhase" "fixupPhase" ]; buildPhase = ''mkdir -p $out/bin; ghc -O2 $src -o $out/bin/nix-script -odir $TMP''; - installPhase = ''ln -s $out/bin/nix-script $out/bin/nix-scripti''; + installPhase = '' + ln -s $out/bin/nix-script $out/bin/nix-scripti + ln -s $out/bin/nix-script $out/bin/nix-scripts + ''; buildInputs = [ (pkgs.haskellPackages.ghcWithPackages (hs: with hs; [posix-escape])) ]; meta = { homepage = https://github.com/bennofs/nix-script; diff --git a/nix-script.hs b/nix-script.hs index 74afe45..25d264b 100755 --- a/nix-script.hs +++ b/nix-script.hs @@ -43,7 +43,7 @@ baseEnv = ["LOCALE_ARCHIVE", "SSL_CERT_FILE" ,"LANG", "TERMINFO", "TERM"] -- | List of supported language definitions languages :: [Language] -languages = [haskell, python, javascript, perl, shell] +languages = [haskell, python 2, python 3, javascript, perl, shell] where haskell = Language "haskell" d r i where d pkgs = pure ("haskellPackages.ghcWithPackages (hs: with hs; [" ++ @@ -51,10 +51,12 @@ languages = [haskell, python, javascript, perl, shell] r script = ("runghc" , [script]) i script = ("ghci" , [script]) - python = Language "python" d r i where - d pkgs = "python" : map ("pythonPackages." ++) pkgs - r script = ("python" , [script]) - i script = ("python" , ["-i", script]) + python v = Language ("python" ++ show v) d r i where + d pkgs = pure ("python" ++ (show v) ++ + ".withPackages (py: with py; [" ++ + unwords pkgs ++ "])") + r script = ("python" ++ show v, [script]) + i script = ("python" ++ show v, ["-i", script]) javascript = Language "javascript" d r i where d pkgs = "node" : map ("nodePackages." ++) pkgs @@ -62,7 +64,8 @@ languages = [haskell, python, javascript, perl, shell] i script = ("node" , []) perl = Language "perl" d r i where - d pkgs = "perl" : map ("perlPackages." ++) pkgs + d pkgs = pure ("perl.withPackages (pl: with pl; [" ++ + unwords pkgs ++ "])") r script = ("perl" , [script]) i script = ("perl" , ["-d", script]) @@ -143,9 +146,12 @@ main = do pkgs = concatMap parseHeader deps language = dropWhile isSpace identifier interactive = last progName == 'i' + shell = last progName == 's' interpreter = makeInter language interactive file cmd <- makeCmd interpreter args <$> makeEnv env - callProcess "nix-shell" ("--pure" : "--command" : cmd : "-p" : pkgs) + if shell + then callProcess "nix-shell" ("-p" : pkgs) + else callProcess "nix-shell" ("--pure" : "--run" : cmd : "-p" : pkgs) - _ -> fail "missing or invalid header" \ No newline at end of file + _ -> fail "missing or invalid header"