Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nix: String and Path Concatenation #nix #nixos

Nix String and Path Concatenation

From Bas van Dijk:

To understand these things I would recommend using nix-repl:

$ nix-repl
Welcome to Nix version 1.11.2. Type :? for help.

nix-repl> name = "bar"

nix-repl> ./foo + "${name}"

Note that the previous is equivalent to the simpler:

nix-repl> ./foo + name

The reason that you get "foobar" is that we didn't include a "/". So lets try to do that:

nix-repl> ./foo/ + name
error: syntax error, unexpected '+', at (string):1:8

Apparently the Nix path parser doesn't like a slash at the end of a path literal. So lets try adding the slash dynamically:

nix-repl> ./foo + "/" + name

What happened here? Well, + is left associative so it is interpreted as:

(./foo + "/") + name

Lets try evaluating that left expression alone:

nix-repl> ./foo + "/"

Apparently Nix performs normalization on paths since the final slash is not included. So lets put the parenthesis differently:

nix-repl> ./foo + ("/" + name)

That's better! Now we can shorten this using some antiquotation:

nix-repl> ./foo + "/${name}"

This comment has been minimized.

Copy link

serokellcao commented Jul 26, 2019


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.