nix-shell: support single quotes in shebangs

Single quotes are a basic feature of shell syntax that people expect to
work. They are also more convenient for writing literal code expressions
with less escaping.
This commit is contained in:
Naïm Favier 2023-06-07 18:12:35 +02:00 committed by Robert Hensing
parent 8b68bbb777
commit fa9642ec45
3 changed files with 24 additions and 10 deletions

View file

@ -235,14 +235,14 @@ package like Terraform:
```bash ```bash
#! /usr/bin/env nix-shell #! /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 terraform apply
``` ```
> **Note** > **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. > in a nix-shell shebang.
Finally, using the merging of multiple nix-shell shebangs the following Finally, using the merging of multiple nix-shell shebangs the following
@ -251,7 +251,7 @@ branch):
```haskell ```haskell
#! /usr/bin/env nix-shell #! /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 #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-20.03.tar.gz
import Network.Curl.Download import Network.Curl.Download

View file

@ -12,4 +12,6 @@
- Introduce a new built-in function [`builtins.convertHash`](@docroot@/language/builtins.md#builtins-convertHash). - Introduce a new built-in function [`builtins.convertHash`](@docroot@/language/builtins.md#builtins-convertHash).
- `nix-shell` shebang lines now support single-quoted arguments.
- `builtins.fetchTree` is now marked as stable. - `builtins.fetchTree` is now marked as stable.

View file

@ -40,7 +40,8 @@ static std::vector<std::string> shellwords(const std::string & s)
std::string cur; std::string cur;
enum state { enum state {
sBegin, sBegin,
sQuote sSingleQuote,
sDoubleQuote
}; };
state st = sBegin; state st = sBegin;
auto it = begin; auto it = begin;
@ -56,15 +57,26 @@ static std::vector<std::string> shellwords(const std::string & s)
} }
} }
switch (*it) { switch (*it) {
case '"': case '\'':
if (st != sDoubleQuote) {
cur.append(begin, it); cur.append(begin, it);
begin = it + 1; begin = it + 1;
st = st == sBegin ? sQuote : sBegin; st = st == sBegin ? sSingleQuote : sBegin;
}
break;
case '"':
if (st != sSingleQuote) {
cur.append(begin, it);
begin = it + 1;
st = st == sBegin ? sDoubleQuote : sBegin;
}
break; break;
case '\\': case '\\':
if (st != sSingleQuote) {
/* perl shellwords mostly just treats the next char as part of the string with no special processing */ /* perl shellwords mostly just treats the next char as part of the string with no special processing */
cur.append(begin, it); cur.append(begin, it);
begin = ++it; begin = ++it;
}
break; break;
} }
} }