Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can Nix language be used for simple shell scripting?

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

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

  2. If yes, is there a way to install only the Nix language on MacOS? What I mean by this is, I don’t want to install Nix’s Multi-User Mode because it sounds like a hassle and intrusive to my computer (AFAIK it requires partitioning my SSD and creating several privileged user accounts).

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

I appears that in the past, there was a Nix Single-User Mode for MacOS. Has anyone found a way to make that previous install method work on MacOS 13.x?

I’m just working on personal development projects.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading