From 368a04fec30837037079d13c3704b9766af37eb8 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 11 Sep 2018 20:07:04 +0200 Subject: add examples --- examples/hello_world.nix | 30 ++++++++++++++++++++++++++++++ examples/simple.nix | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 examples/hello_world.nix create mode 100644 examples/simple.nix diff --git a/examples/hello_world.nix b/examples/hello_world.nix new file mode 100644 index 0000000..4bb3db1 --- /dev/null +++ b/examples/hello_world.nix @@ -0,0 +1,30 @@ +let + pkgs = import { overlays = [ (import ../pkgs) ]; }; +in { + bash = pkgs.writeBash "hello-world" '' + echo 'hello world' + ''; + c = pkgs.writeC "hello-world" {} '' + #include + int main() { + printf("hello world\n"); + return 0; + } + ''; + dash = pkgs.writeDash "hello-world" '' + echo 'hello world' + ''; + haskell = pkgs.writeHaskell "hello-world" [] '' + main = do + putStrLn "hello world" + ''; + python2 = pkgs.writePython2 "hello-world" {} '' + print "hello world" + ''; + python3 = pkgs.writePython3 "hello-world" {} '' + print("hello world") + ''; + sed = pkgs.writeDash "sed-example" '' + echo xxx | ${pkgs.writeSed "hello-world" "s/xxx/hello world/"} + ''; +} diff --git a/examples/simple.nix b/examples/simple.nix new file mode 100644 index 0000000..32a4cee --- /dev/null +++ b/examples/simple.nix @@ -0,0 +1,38 @@ +let + pkgs = import { overlays = [ (import ../pkgs) ]; }; +in { + bash = pkgs.writeBash "hello-world" '' + if [[ "test" == "test" ]]; then echo "bash features"; fi + ''; + # cc -L/nix/store/...blah/lib -I/nix/store/...blah/include + dash = pkgs.writeDash "hello-world" '' + test '~' = '~' && echo 'dash features' + ''; + haskell = pkgs.writeHaskell "hello-world" [ "acme-cuteboy" ] '' + import Acme.CuteBoy + + main :: IO () + main = print Rolf + ''; + python2 = pkgs.writePython2 "hello-world" { deps = [ pkgs.python2Packages.pyyaml ]; } '' + import yaml + + print yaml.load(""" + - some + - random + - variables + """) + ''; + python3 = pkgs.writePython3 "hello-world" { deps = [ pkgs.python3Packages.pyyaml ]; } '' + import yaml + + print(yaml.load(""" + - some + - random + - variables + """)) + ''; + sed = pkgs.writeDash "sed-example" '' + echo hello | ${pkgs.writeSed "hello-world" "s/hello/& world/"} + ''; +} -- cgit v1.2.3 From 9cd634f650551080b01e06763c11836ce33e0613 Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 12 Sep 2018 14:57:09 +0200 Subject: pkgs: add writePerl{,Bin} --- examples/hello_world.nix | 3 +++ examples/simple.nix | 4 ++++ pkgs/default.nix | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/examples/hello_world.nix b/examples/hello_world.nix index 4bb3db1..4944c13 100644 --- a/examples/hello_world.nix +++ b/examples/hello_world.nix @@ -18,6 +18,9 @@ in { main = do putStrLn "hello world" ''; + perl = pkgs.writePerl "hello-world" {} '' + print "hello world\n"; + ''; python2 = pkgs.writePython2 "hello-world" {} '' print "hello world" ''; diff --git a/examples/simple.nix b/examples/simple.nix index 32a4cee..ad62b84 100644 --- a/examples/simple.nix +++ b/examples/simple.nix @@ -14,6 +14,10 @@ in { main :: IO () main = print Rolf ''; + perl = pkgs.writePerl "simple.pl" { deps = [ pkgs.perlPackages.boolean ]; } '' + use boolean; + print "Howdy!\n" if true; + ''; python2 = pkgs.writePython2 "hello-world" { deps = [ pkgs.python2Packages.pyyaml ]; } '' import yaml diff --git a/pkgs/default.nix b/pkgs/default.nix index 29457ca..b7b0dde 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -313,6 +313,23 @@ pkgs: oldpkgs: { ${pkgs.cabal2nix}/bin/cabal2nix ${path} > $out ''); + writePerl = name: { deps ? [] }: + let + perl-env = pkgs.buildEnv { + name = "perl-environment"; + paths = deps; + pathsToLink = [ + "/lib/perl5/site_perl" + ]; + }; + in + pkgs.makeScriptWriter { + interpreter = "${pkgs.perl}/bin/perl -I ${perl-env}/lib/perl5/site_perl"; + } name; + + writePerlBin = name: + pkgs.writePerl "/bin/${name}"; + writePython2 = name: { deps ? [], flakeIgnore ? [] }: let py = pkgs.python2.withPackages (ps: deps); -- cgit v1.2.3 From 16b43f218d142de49fbd8fae77e6e2c057bfb9d6 Mon Sep 17 00:00:00 2001 From: lassulus Date: Wed, 12 Sep 2018 22:26:41 +0200 Subject: add ci.nix --- ci.nix | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ci.nix diff --git a/ci.nix b/ci.nix new file mode 100644 index 0000000..545121e --- /dev/null +++ b/ci.nix @@ -0,0 +1,26 @@ +let + pkgs = import {}; + + hello_worlds = import examples/hello_world.nix; + simples = import examples/simple.nix; + + writeTest = expectedValue: test: pkgs.writeScript "test" '' + #!/bin/sh + if test "$(${test})" != "${expectedValue}"; then + echo 'test ${test} failed' + exit 1 + fi + ''; + +in + pkgs.lib.mapAttrs' (n: v: pkgs.lib.nameValuePair "hello_${n}" (writeTest "hello world" v)) hello_worlds // + pkgs.lib.mapAttrs' (n: v: pkgs.lib.nameValuePair "simple_${n}" v) { + bash = writeTest "bash features" simples.bash; + dash = writeTest "dash features" simples.dash; + haskell = writeTest "Rolf" simples.haskell; + perl = writeTest "Howdy!" simples.perl; + python2 = writeTest "['some', 'random', 'variables']" simples.python2; + python3 = writeTest "['some', 'random', 'variables']" simples.python3; + sed = writeTest "hello world" simples.sed; + } + -- cgit v1.2.3 From 3212b4fbfb6fff4a1f133b9e18b84f298c6da65d Mon Sep 17 00:00:00 2001 From: lassulus Date: Sun, 23 Sep 2018 12:45:48 +0200 Subject: examples/simple: more descriptive names --- examples/simple.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/simple.nix b/examples/simple.nix index ad62b84..0284bf1 100644 --- a/examples/simple.nix +++ b/examples/simple.nix @@ -1,14 +1,14 @@ let pkgs = import { overlays = [ (import ../pkgs) ]; }; in { - bash = pkgs.writeBash "hello-world" '' + bash = pkgs.writeBash "simple.sh" '' if [[ "test" == "test" ]]; then echo "bash features"; fi ''; # cc -L/nix/store/...blah/lib -I/nix/store/...blah/include - dash = pkgs.writeDash "hello-world" '' + dash = pkgs.writeDash "simple" '' test '~' = '~' && echo 'dash features' ''; - haskell = pkgs.writeHaskell "hello-world" [ "acme-cuteboy" ] '' + haskell = pkgs.writeHaskell "simple" [ "acme-cuteboy" ] '' import Acme.CuteBoy main :: IO () @@ -18,7 +18,7 @@ in { use boolean; print "Howdy!\n" if true; ''; - python2 = pkgs.writePython2 "hello-world" { deps = [ pkgs.python2Packages.pyyaml ]; } '' + python2 = pkgs.writePython2 "simple.py" { deps = [ pkgs.python2Packages.pyyaml ]; } '' import yaml print yaml.load(""" @@ -27,7 +27,7 @@ in { - variables """) ''; - python3 = pkgs.writePython3 "hello-world" { deps = [ pkgs.python3Packages.pyyaml ]; } '' + python3 = pkgs.writePython3 "simple.py" { deps = [ pkgs.python3Packages.pyyaml ]; } '' import yaml print(yaml.load(""" -- cgit v1.2.3 From 79ab2c27f9d7903ff3ed9bcd8de493b1191bbc12 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sun, 23 Sep 2018 13:05:11 +0200 Subject: pkgs: add writeJS{,Bin} --- ci.nix | 1 + examples/hello_world.nix | 3 +++ examples/simple.nix | 6 ++++++ pkgs/default.nix | 17 +++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/ci.nix b/ci.nix index 545121e..902d43c 100644 --- a/ci.nix +++ b/ci.nix @@ -18,6 +18,7 @@ in bash = writeTest "bash features" simples.bash; dash = writeTest "dash features" simples.dash; haskell = writeTest "Rolf" simples.haskell; + js = writeTest "function add(n,d){return n+d}" simples.js; perl = writeTest "Howdy!" simples.perl; python2 = writeTest "['some', 'random', 'variables']" simples.python2; python3 = writeTest "['some', 'random', 'variables']" simples.python3; diff --git a/examples/hello_world.nix b/examples/hello_world.nix index 4944c13..4f9330a 100644 --- a/examples/hello_world.nix +++ b/examples/hello_world.nix @@ -18,6 +18,9 @@ in { main = do putStrLn "hello world" ''; + js = pkgs.writeJS "hello-world" {} '' + console.log("hello world") + ''; perl = pkgs.writePerl "hello-world" {} '' print "hello world\n"; ''; diff --git a/examples/simple.nix b/examples/simple.nix index 0284bf1..55ccedc 100644 --- a/examples/simple.nix +++ b/examples/simple.nix @@ -14,6 +14,12 @@ in { main :: IO () main = print Rolf ''; + js = pkgs.writeJS "example-js" { deps = [ pkgs.nodePackages.uglify-js ]; } '' + var UglifyJS = require("uglify-js"); + var code = "function add(first, second) { return first + second; }"; + var result = UglifyJS.minify(code); + console.log(result.code); + ''; perl = pkgs.writePerl "simple.pl" { deps = [ pkgs.perlPackages.boolean ]; } '' use boolean; print "Howdy!\n" if true; diff --git a/pkgs/default.nix b/pkgs/default.nix index b7b0dde..10465a6 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -297,6 +297,23 @@ pkgs: oldpkgs: { }; }; + writeJS = name: { deps ? [] }: text: + let + node-env = pkgs.buildEnv { + name = "node"; + paths = deps; + pathsToLink = [ + "/lib/node_modules" + ]; + }; + in pkgs.writeDash name '' + export NODE_PATH=${node-env}/lib/node_modules + exec ${pkgs.nodejs}/bin/node ${pkgs.writeText "js" text} + ''; + + writeJSBin = name: + pkgs.writeJS "/bin/${name}"; + writeJSON = name: value: pkgs.runCommand name { json = toJSON value; passAsFile = [ "json" ]; -- cgit v1.2.3