Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shebang flakes v2 #8327

Merged
merged 17 commits into from
Nov 7, 2023
Merged

Shebang flakes v2 #8327

merged 17 commits into from
Nov 7, 2023

Conversation

roberth
Copy link
Member

@roberth roberth commented May 12, 2023

Motivation

This adds shebang support as implemented by @tomberek with the changes/additions

  • Double backtick quoting only. This keeps things simple, while also delivering a powerful syntax for self contained scripts (one file) with custom expressions.
  • Relative paths are relative to the script. Otherwise, they'd be relative to the working directory, which is would be unpredictable and therefore less useful.
  • It reserves syntax in the shebang lines; in other words, some symbols demand quotes. This has two benefits
    • The syntax looks unambiguous. Quoted symbols look like literal symbols, and they are.
    • The syntax can be extended later, without breaking scripts that used to work.

Remaining todo:

  • last commit makes flake flags relative to script dir. tests?
  • should some commands add an implicit --? All commands or just run/shell/develop?

Context

Based on

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • documentation in the internal API docs
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

Priorities

Add 👍 to pull requests you find important.

@github-actions github-actions bot added documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority labels May 12, 2023
@roberth roberth mentioned this pull request May 12, 2023
17 tasks
Copy link
Contributor

@fricklerhandwerk fricklerhandwerk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a superficial perspective of a potential user and manual reader, I mainly have two questions:

  1. Why is that "weird" escaping needed? If it's technically unavoidable it sure needs the reason documented. Otherwise it appears arbitrary and cumbersome.
  2. Why is it nix shell? What does that have to do with shells at all?

I understand that historically stuff was just tacked onto nix-shell being a kitchen sink, but this is a different situation. Could we do something like #! nix shebang ... --interpreter <command> which could be shortened with -i?

Or even #! nix interpret <command> ... where the command is positional?

Comment on lines 3 to 4
* The experimental nix command is now a `#!-interpreter` by appending the
contents of any `#! nix` lines and the script's location to a single call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The experimental nix command is now a `#!-interpreter` by appending the
contents of any `#! nix` lines and the script's location to a single call.
* The experimental `nix` command is now a `#!-interpreter` by concatenating the
contents of any `#! nix` lines and the script's location into a single call to ???.

Is that what you mean? What gets called? This isn't clear.

doc/manual/src/release-notes/rl-next.md Outdated Show resolved Hide resolved
src/nix/shell.md Show resolved Hide resolved
src/nix/shell.md Show resolved Hide resolved
src/nix/shell.md Show resolved Hide resolved
src/nix/shell.md Show resolved Hide resolved
> You must use double backticks (```` `` ````) when passing a simple Nix expression
> in a nix shell shebang.

Finally, using the merging of multiple nix shell shebangs the following
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this merging something? I don't understand how it relates to the example. It looks exactly like the other ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple lines of "#!nix" are concatenated after that prefix is removed. This allows breaking up a long line into multiple lines.

Comment on lines +135 to +145
import Network.Curl.Download
import Text.HTML.TagSoup
import Data.Either
import Data.ByteString.Char8 (unpack)

-- Fetch nixos.org and print all hrefs.
main = do
resp <- openURI "https://nixos.org/"
let tags = filter (isTagOpenName "a") $ parseTags $ unpack $ fromRight undefined resp
let tags' = map (fromAttrib "href") tags
mapM_ putStrLn $ filter (/= "") tags'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty much arbitrary stuff just to show the shebang mechanism. The actual scripts should aim to be one liners. Everything else will be noise to 90% of the readers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

history: I tried to copy the previous examples and modify as little as possible. I am not opposed to a simpler script if that would communicate the concept better.

src/nix/shell.md Show resolved Hide resolved
@thufschmitt thufschmitt added the idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. label May 26, 2023
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-05-26-nix-team-meeting-minutes-58/28572/1

Copy link
Contributor

@tomberek tomberek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining items are release notes and docs updates from @fricklerhandwerk

@tomberek
Copy link
Contributor

I started trying to incorporate the suggestions. (hercules-ci@5d33afe)

tests/flakes/flakes.sh Outdated Show resolved Hide resolved
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix3-equivilents-to-nix-shell-i-python-p-hello/34417/2

@github-actions github-actions bot removed the with-tests Issues related to testing. PRs with tests have some priority label Oct 23, 2023
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Oct 23, 2023
tomberek and others added 8 commits November 6, 2023 17:07
Enables shebang usage of nix shell. All arguments with `#! nix` get
added to the nix invocation. This implementation does NOT set any
additional arguments other than placing the script path itself as the
first argument such that the interpreter can utilize it.

Example below:

```
    #!/usr/bin/env nix
    #! nix shell --quiet
    #! nix nixpkgs#bash
    #! nix nixpkgs#shellcheck
    #! nix nixpkgs#hello
    #! nix --ignore-environment --command bash
    # shellcheck shell=bash
    set -eu
    shellcheck "$0" || exit 1
    function main {
        hello
        echo 0:"$0" 1:"$1" 2:"$2"
    }
    "$@"
```

fix: include programName usage

EDIT: For posterity I've changed shellwords to shellwords2 in order
      not to interfere with other changes during a rebase.
      shellwords2 is removed in a later commit. -- roberth
This will allow a different base directory to be used, matching
a shebang script location instead of the working directory.
@tomberek tomberek merged commit ab69dc4 into NixOS:master Nov 7, 2023
11 checks passed
@Ericson2314
Copy link
Member

Ericson2314 commented Nov 7, 2023

FYI perhaps we never made this policy, but this was merged (a) without a merge commit and (b) with commits that perhaps don't individually pass tests (my guess from the messages). The combination of those two things makes bisecting later harder. (Merge commit helps with more complicated history because --first-parent allows skipping the PR history.)

@tomberek
Copy link
Contributor

tomberek commented Nov 8, 2023

FYI perhaps we never made this policy, but this was merged (a) without a merge commit and (b) with commits that perhaps don't individually pass tests (my guess from the messages). The combination of those two things makes bisecting later harder. (Merge commit helps with more complicated history because --first-parent allows skipping the PR history.)

Worth a revert and re-merge?

@roberth
Copy link
Member Author

roberth commented Nov 10, 2023

Three PRs have been merged since, and would have to be remerged, plus you get the general nastiness of force-pushing to master. I'd say leave as is, but pay attention to how you're merging PRs.
Assuming you're using the green button, that thing is stateful. It remembers which way you merged the last one, so just always leave it at "merge" and never micro optimize by using rebase for single commit PRs.
Squashing should be done with the CLI, preferably by the author.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-shell-with-shebang/17088/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

8 participants