-2

I am intrigued by the Nix language, because it is functional and because file paths are first class data types.

Can the Nix language be used, in a stand-alone capacity, for simple shell scripting, specifically for calling typical command-line utils (such as compilers) and copying/moving files?

I do not intend to use it in a production environment. Only for personal use.

devdanke
  • 1,309
  • 15
  • 27
  • Also I would ask the mac related question, in another question post. – Chris Stryczynski Feb 12 '23 at 14:46
  • @KarlKnechtel In this case, I believe asking about the _suitability_ of Nix for shell script is legitimate and valuable for Stackoverflow, because Nix is not widely used and not very well documented. If you had any knowledge of _Nix_ you would know this. A more generous reading of my question would have been "can it be used as a typical scripting language". That's a simple, objective yes or no question. Part two of the question is also clearly asking for an objective answer. – devdanke Feb 12 '23 at 15:08
  • No, it isn't. "Scripting language" isn't well defined, in the first place. – Karl Knechtel Feb 12 '23 at 15:16
  • ? I consider that your question is off topic for Stack Overflow, and I commented to give my reasoning as to why. If that's "unfriendly" to you then I don't know what to tell you. Communities are allowed to have standards. – Karl Knechtel Feb 13 '23 at 03:21

1 Answers1

1

Is Nix language suitable for simple shell scripting, such as calling typical command-line utils and copying/moving files?

It's not really a general purpose language. And even writing 'scripts' in Nix, you would use a regular scripting language like bash. The Nix language is mostly focused on interacting with a nix store.

Nix always get executed in an indepedent/sandboxed enviornment, similar to the docker build environment. As such it won't have access to your usual filesystem. And that is kind of the entire point of Nix.

However I think that is an implementation detail and technically I don't think there is anything limiting Nix from running in the context of your native environment. This would have to be developed, which is not a small job.


However saying that you might still find nix useful for writing a shell script. You could write a nix expression that builds a nix package.

For example (just roughly, haven't checked this):

example = (import (builtins.fetchGit {
  url = "ssh://root@example.co.uk:/root/gitrepo/example";
  rev = "abcxyz"
}) {});


pkgs.writeScript "example.sh"
        ''
            #!${pkgs.stdenv.shell}
            set -e

            echo "hi"
            cat ${example}/docs/example
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • 2
    More specifically, the only thing the Nix language is used for is to define *derivatives*, which act as input to commands like `nix-build` which actually execute commands specified in the derivative. Nix itself (IIRC) has no facility to execute anything directly. – chepner Feb 12 '23 at 14:20
  • I stumbed across some snipets that made _Nix_ look like a hybrid of OCaml and Lisp, such as https://nixcloud.io/tour. – devdanke Feb 12 '23 at 14:28
  • 1
    It could look similar, but it's execution environment is the main reason it's not a general purpose scripting language. – Chris Stryczynski Feb 12 '23 at 14:35
  • I was hoping to find out if _Nix language_ had been or could be broken out into a separate installable. Your answer indicates it hasn't. It looks like _Nix_ is part of a tightly integrated devOps tool. Thanks for your helpful, detailed answer. – devdanke Feb 12 '23 at 15:18