Nix-writers: Difference between revisions

From NixOS Wiki
imported>Lassulus
Add nix-writers skeleton
 
imported>Lassulus
No edit summary
Line 20: Line 20:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
pkgs.writers.writeC "hello-world-ncurses" { libraries = [ pkgs.ncurses ]; } ''
pkgs.writers.writeC "hello-world-ncurses" {
  libraries = [ pkgs.ncurses ];
} ''
   #include <ncurses.h>
   #include <ncurses.h>
   int main() {
   int main() {
Line 41: Line 43:


===Haskell===
===Haskell===
<syntaxHighlight lang=nix>
writeHaskell "missiles" {
  libraries = [ pkgs.haskellPackages.acme-missiles ];
} ''
  import Acme.Missiles
  main = launchMissiles
''
</syntaxHighlight>


===JavaScript===
===JavaScript===
<syntaxHighlight lang=nix>
writeJS "example" {
  libraries = [ 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);
''
</syntaxHighlight>


===Perl===
===Perl===
<syntaxHighlight lang=nix>
writePerl "example" {
  libraries = [ pkgs.perlPackages.boolean ];
} ''
  use boolean;
  print "Howdy!\n" if true;
''
</syntaxHighlight>


===Python2===
===Python2===
<syntaxHighlight lang=nix>
writePython2 "test_python2" {
  libraries = [ pkgs.python2Packages.enum ];
} ''
  from enum import Enum
  class Test(Enum):
      a = "success"
  print Test.a
''
</syntaxHighlight>


===Python3===
===Python3===
<syntaxHighlight lang=nix>
writePython3 "test_python3" {
  libraries = [ pkgs.python3Packages.pyyaml ];
} ''
  import yaml
  y = yaml.load("""
    - test: success
  """)
  print(y[0]['test'])
''
</syntaxHighlight>

Revision as of 09:36, 22 April 2019

Nix-writers are a way to write other programming languages inline in nix-code. Basically it's like writeScript/writeScriptBin but for other Languages.

Every writer has a ...Bin variant which can be used inside environment.systemPackages. Most of the writers take an attrributeset where one can add libraries.

Languages

bash

This is basically writeScript but with the shebang to bash already included.

pkgs.writers.writeBash "hello_world" ''
  echo 'hello world!'
''

C

pkgs.writers.writeC "hello-world-ncurses" {
  libraries = [ pkgs.ncurses ];
} ''
  #include <ncurses.h>
  int main() {
    initscr();
    printw("Hello World !!!");
    refresh(); endwin();
    return 0;
  }
''

dash

pkgs.writers.writeDash "hello_world" ''
  echo 'hello world!'
''


Haskell

writeHaskell "missiles" {
  libraries = [ pkgs.haskellPackages.acme-missiles ];
} ''
  import Acme.Missiles

  main = launchMissiles
''

JavaScript

writeJS "example" {
  libraries = [ 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

writePerl "example" {
  libraries = [ pkgs.perlPackages.boolean ];
} ''
  use boolean;
  print "Howdy!\n" if true;
''

Python2

writePython2 "test_python2" {
  libraries = [ pkgs.python2Packages.enum ];
} ''
  from enum import Enum

  class Test(Enum):
      a = "success"

  print Test.a
''

Python3

writePython3 "test_python3" {
  libraries = [ pkgs.python3Packages.pyyaml ];
} ''
  import yaml

  y = yaml.load("""
    - test: success
  """)
  print(y[0]['test'])
''