Skip to content

dmyTRUEk/nixla

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nixla - nix as a programming language

Love nix? Now you can use it as a programming language also!

nixla variants:

  1. default - returns a string
  2. nix - returns a nix expression
  3. json - returns a json

Examples:

Example 1: add input to itself

Create file:

# add.nix
input: input + input

Run it like this:

$ nixla add.nix Hello
HelloHello

Or like this:

$ echo Hello | nixla add.nix
HelloHello

Example 2: multiply all numbers

# mul_all_numbers.nix
let
  inherit (builtins) filter split foldl' fromJSON;
  split_ = sep_regex: str:
    filter
      (el: el != [])
      (split sep_regex str);
in
  input:
  foldl'
    (acc: el: acc * el)
    1
    (map
      fromJSON # works as toInt
      (split_ " " input)
    )

Run it:

$ nixla-nix mul_all_numbers.nix 2 3 7
42

Or using pipes:

$ echo 2 3 7 | nixla-nix mul_all_numbers.nix
42

Example 3: import

# mylib.nix
{
  add = input: input + input;
}
# import-mylib.nix
let
  inherit (import ./mylib.nix) add;
in
  input:
  add input

Run it:

$ nixla import-mylib.nix Hello
HelloHello

Other examples:

About

nix as a programming language

Topics

Resources

License

Stars

Watchers

Forks