Skip to content

Commit

Permalink
Merge pull request NixOS#8470 from ncfavier/shebang-single-quotes
Browse files Browse the repository at this point in the history
nix-shell: support single quotes in shebangs, fix whitespace parsing
(cherry picked from commit 3b99c62)
Change-Id: I2a431b21c3467eefa1ef95d5a36d672f45b6937a
  • Loading branch information
eldritch horrors committed Mar 4, 2024
1 parent 7f590ea commit 032eff7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
6 changes: 3 additions & 3 deletions doc/manual/src/command-ref/nix-shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ package like Terraform:

```bash
#! /usr/bin/env nix-shell
#! nix-shell -i bash --packages "terraform.withPlugins (plugins: [ plugins.openstack ])"
#! nix-shell -i bash --packages 'terraform.withPlugins (plugins: [ plugins.openstack ])'

terraform apply
```

> **Note**
>
> You must use double quotes (`"`) when passing a simple Nix expression
> You must use single or double quotes (`'`, `"`) when passing a simple Nix expression
> in a nix-shell shebang.
Finally, using the merging of multiple nix-shell shebangs the following
Expand All @@ -251,7 +251,7 @@ branch):

```haskell
#! /usr/bin/env nix-shell
#! nix-shell -i runghc --packages "haskellPackages.ghcWithPackages (ps: [ps.download-curl ps.tagsoup])"
#! nix-shell -i runghc --packages 'haskellPackages.ghcWithPackages (ps: [ps.download-curl ps.tagsoup])'
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-20.03.tar.gz

import Network.Curl.Download
Expand Down
2 changes: 2 additions & 0 deletions doc/manual/src/release-notes/rl-next.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Release X.Y (202?-??-??)

- `nix-shell` shebang lines now support single-quoted arguments.
38 changes: 26 additions & 12 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ extern char * * environ __attribute__((weak));
*/
static std::vector<std::string> shellwords(const std::string & s)
{
std::regex whitespace("^(\\s+).*");
std::regex whitespace("^\\s+");
auto begin = s.cbegin();
std::vector<std::string> res;
std::string cur;
enum state {
sBegin,
sQuote
sSingleQuote,
sDoubleQuote
};
state st = sBegin;
auto it = begin;
Expand All @@ -50,26 +51,39 @@ static std::vector<std::string> shellwords(const std::string & s)
if (regex_search(it, s.cend(), match, whitespace)) {
cur.append(begin, it);
res.push_back(cur);
cur.clear();
it = match[1].second;
it = match[0].second;
if (it == s.cend()) return res;
begin = it;
cur.clear();
}
}
switch (*it) {
case '\'':
if (st != sDoubleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sSingleQuote : sBegin;
}
break;
case '"':
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sQuote : sBegin;
if (st != sSingleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sDoubleQuote : sBegin;
}
break;
case '\\':
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
if (st != sSingleQuote) {
/* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it);
begin = ++it;
}
break;
}
}
if (st != sBegin) throw Error("unterminated quote in shebang line");
cur.append(begin, it);
if (!cur.empty()) res.push_back(cur);
res.push_back(cur);
return res;
}

Expand Down Expand Up @@ -128,7 +142,7 @@ static void main_nix_build(int argc, char * * argv)
for (auto line : lines) {
line = chomp(line);
std::smatch match;
if (std::regex_match(line, match, std::regex("^#!\\s*nix-shell (.*)$")))
if (std::regex_match(line, match, std::regex("^#!\\s*nix-shell\\s+(.*)$")))
for (const auto & word : shellwords(match[1].str()))
args.push_back(word);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/nix-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ chmod a+rx $TEST_ROOT/spaced\ \\\'\"shell.shebang.rb
output=$($TEST_ROOT/spaced\ \\\'\"shell.shebang.rb abc ruby)
[ "$output" = '-e load(ARGV.shift) -- '"$TEST_ROOT"'/spaced \'\''"shell.shebang.rb abc ruby' ]

# Test nix-shell shebang quoting
sed -e "s|@ENV_PROG@|$(type -P env)|" shell.shebang.nix > $TEST_ROOT/shell.shebang.nix
chmod a+rx $TEST_ROOT/shell.shebang.nix
$TEST_ROOT/shell.shebang.nix

# Test 'nix develop'.
nix develop -f "$shellDotNix" shellDrv -c bash -c '[[ -n $stdenv ]]'

Expand Down
10 changes: 10 additions & 0 deletions tests/functional/shell.shebang.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! @ENV_PROG@ nix-shell
#! nix-shell -I nixpkgs=shell.nix --no-substitute
#! nix-shell --argstr s1 'foo "bar" \baz'"'"'qux' --argstr s2 "foo 'bar' \"\baz" --argstr s3 \foo\ bar\'baz --argstr s4 ''
#! nix-shell shell.shebang.nix --command true
{ s1, s2, s3, s4 }:
assert s1 == ''foo "bar" \baz'qux'';
assert s2 == "foo 'bar' \"baz";
assert s3 == "foo bar'baz";
assert s4 == "";
(import <nixpkgs> {}).runCommand "nix-shell" {} ""

0 comments on commit 032eff7

Please sign in to comment.