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

Add nix flake #1908

Merged
merged 2 commits into from
Jun 28, 2024
Merged

Add nix flake #1908

merged 2 commits into from
Jun 28, 2024

Conversation

fidgetingbits
Copy link
Collaborator

Adds a flake I have been using for development on a Nix box, which I've confirmed works to run pre-commit linting, correctly installs plugins, can debug extension, etc.

Checklist

  • [-] I have added tests
  • [-] I have updated the docs and cheatsheet
  • [-] I have not broken the cheatsheet

flake.nix Outdated Show resolved Hide resolved
flake.nix Outdated Show resolved Hide resolved
@pokey
Copy link
Member

pokey commented Sep 27, 2023

Sorry I'm not familiar with nix flake. What is this PR doing?

1 similar comment
@pokey

This comment was marked as duplicate.

@fidgetingbits
Copy link
Collaborator Author

Nix flakes are basically packaging management files (written in the nix functional programming language) that projects can add that allow the nix package manager to automatically setup a shell environment of locked packages, without globally installing them. So basically someone can just activate the flake development environment and everything needed to develop/build is automatically installed there.

Assuming a project is something actually packaged like an installable tool, you can also have the flake describe how to build the package for you so you can install/run it. Not sure how that works for vscode extension installation in nix and didn't bother looking yet.

Nix is used for package package management by Mac (replacement for homebrew) and Linux users wanting fully reproducible environments, and also is what NixOS is centered around using.

I'm doing the cursorless hacking from a NixOS setup, so just threw it together and figured I'd send a PR in case it would be useful for other nix users that might hack on cursorless in the future. That said, I totally appreciate if you don't want to add this :D

flake.nix Outdated Show resolved Hide resolved
@pokey
Copy link
Member

pokey commented Sep 29, 2023

Reproducibility is my middle name. I'll have a look

@auscompgeek
Copy link
Member

I did try out this PR on x86 macOS, and it appears to work as expected, with the known caveat that if you run VSCode within a nix environment then the command server and Talon command client will fail to find each other.

I'll admit I didn't test it for too long though, and still need to check the pre-commit hook installed correctly (but that looks correct 😅)

@fidgetingbits
Copy link
Collaborator Author

Is that known caveat detailed anywhere? Is it Mac specific? I don't have that problem on NixOS afaik, as I've been using talon/cursorless for the development.

The pre-commit hooks seemed to install for me and run fine when I sent the other PRs but definitely worth someone else verifying.

@auscompgeek
Copy link
Member

auscompgeek commented Sep 29, 2023

I noted it somewhere once I worked out the mismatching $TMPDIR was the issue. Might be mentioned in the talonhub/community issues.

Edit: here we are: talonhub/community#966 (comment)

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

I am going to completely defer to @auscompgeek to review this one, as I'm way out of my depth. I left one comment tho to give the appearance of being useful / competent 🤓

flake.nix Outdated Show resolved Hide resolved
@fidgetingbits
Copy link
Collaborator Author

Since I just had to force push again (I seem to do this a lot in my PRs), I'm curious if someone can tell me the trick to merge upstream changes into the PR branch without it triggering like a thousand file updates? I don't seem to easily find an explanation of what I'm missing, but you folks presumably know. I do know about the button in the github interface, but presumably there is some "right" cli way

@auscompgeek
Copy link
Member

I think you're just seeing GitHub's eventual consistency behaviour? I see 1 commit with 3 files changed, which I presume is expected.

@fidgetingbits
Copy link
Collaborator Author

I think you're just seeing GitHub's eventual consistency behaviour? I see 1 commit with 3 files changed, which I presume is expected.

Hrm, I'll look into what that is.

But in this case it's only the 3 files changed because I force pushed. I did a soft reset back to a shared commit with upstream, rebase, then apply a new commit with all my changes and force push.

@pokey
Copy link
Member

pokey commented Mar 11, 2024

@auscompgeek are you happy with this PR? As mentioned above I don't know enough about nix to be a competent reviewer here

flake.nix Outdated Show resolved Hide resolved
@fidgetingbits
Copy link
Collaborator Author

I'll start tweaking this again so I can use it to build/test neovim, but it would still be nice to get it in as is in the meantime, as I still have to pull it into every new branch I create atm if I want to test anything. Happy to change anything else needed.

@jaresty
Copy link
Contributor

jaresty commented Jun 13, 2024

Haven't tried this but I think it's a great idea. Maybe I'll try nix again in the future.

flake.nix Outdated Show resolved Hide resolved
@pokey
Copy link
Member

pokey commented Jun 14, 2024

@auscompgeek I'm just waiting for an approval from you on this one and I'll merge

@fidgetingbits
Copy link
Collaborator Author

Looks like switching to corepack breaks one of the pre-commit hooks. Wasn't something I had tested after switching. Will revert that change and just stick with hardcoded node 18 package for now I think. If it breaks in the future because someone updates package.json then I'll fix the flake. Doesn't seem any different than corepack breaking things now, so I'd rather just stick to what works atm. Can see more context here

That said, there is another pre-commit hook related bug introduced as of March, that both versions of the flake, which is that the prettier hook now has an external npm dependency that pre-commit itself won't auto install, as it's specific to .prettierc config, so I now need to hack around that until I get an associated prettier-plugin-tailwindcss package upstreamed in nixpkgs. Will fix that and update this PR, but means merging this PR will have to wait.

@fidgetingbits
Copy link
Collaborator Author

Looks like switching to corepack breaks one of the pre-commit hooks. Wasn't something I had tested after switching. Will revert that change and just stick with hardcoded node 18 package for now I think. If it breaks in the future because someone updates package.json then I'll fix the flake. Doesn't seem any different than corepack breaking things now, so I'd rather just stick to what works atm. Can see more context here

Did some more testing while fixing the tailwind issue and it seems like the chai issue was due to me not running pre-commit clean after I switched to corepack. Seems like it does in fact work, so I won't revert after all.

I ran pre-commit run -a and noticed some other scripts that will break due to missing python dependencies, so also now add those to the packages. tbh I don't understand why those aren't being installed by pre-commit, because the dependencies don't seem to be used directly by cursorless. I also rebased and squashed the 3 commits that were here, so now just back to one.

flake.nix Outdated Show resolved Hide resolved
flake.nix Outdated Show resolved Hide resolved
@fidgetingbits
Copy link
Collaborator Author

When I re-run direnv allow, now that pnpm install is used, I get warning spam:

❯ direnv allow
direnv: loading ~/dev/talon/fidgetingbits-cursorless/.envrc                                                                                                                                                                      
direnv: using flake
direnv: nix-direnv: using cached dev shell
Scope: all 12 workspace projects
Lockfile is up to date, resolution step is skipped
Already up to date
 WARN  Failed to create bin at /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/jest-config@29.7.0_@types+node@18.18.2_ts-node@10.9.2_@types+node@18.18.2_typescript@5.4.3_/node_modules/jest-config/node_modules/.
bin/ts-node. ENOENT: no such file or directory, chmod '/home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/jest-config@29.7.0_@types+node@18.18.2_ts-node@10.9.2_@types+node@18.18.2_typescript@5.4.3_/node_modules/je
st-config/node_modules/.bin/ts-node'
 WARN  Failed to create bin at /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/ts-jest@29.1.2_@babel+core@7.24.3_@jest+types@29.6.3_babel-jest@29.7.0_@babel+core@7.24.3__es_x5o3lutkgkkiogdpvhimm74uee/node_modul
es/ts-jest/node_modules/.bin/jest. ENOENT: no such file or directory, chmod '/home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/ts-jest@29.1.2_@babel+core@7.24.3_@jest+types@29.6.3_babel-jest@29.7.0_@babel+core@7.
24.3__es_x5o3lutkgkkiogdpvhimm74uee/node_modules/ts-jest/node_modules/.bin/jest'
 WARN  Failed to create bin at /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/html-webpack-plugin@5.6.0_webpack@5.91.0_esbuild@0.20.2_webpack-cli@5.1.4_@webpack-cli+genera_o5z57zhf57uxuhfj26re5e4zha/node_modul
es/html-webpack-plugin/node_modules/.bin/html-minifier-terser. ENOENT: no such file or directory, chmod '/home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/html-webpack-plugin@5.6.0_webpack@5.91.0_esbuild@0.20.2_w
ebpack-cli@5.1.4_@webpack-cli+genera_o5z57zhf57uxuhfj26re5e4zha/node_modules/html-webpack-plugin/node_modules/.bin/html-minifier-terser'
 WARN  Failed to create bin at /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/ts-jest@29.1.2_@babel+core@7.24.3_@jest+types@29.6.3_babel-jest@29.7.0_@babel+core@7.24.3__es_x5o3lutkgkkiogdpvhimm74uee/node_modul
es/ts-jest/node_modules/.bin/semver. ENOENT: no such file or directory, chmod '/home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/ts-jest@29.1.2_@babel+core@7.24.3_@jest+types@29.6.3_babel-jest@29.7.0_@babel+core@
7.24.3__es_x5o3lutkgkkiogdpvhimm74uee/node_modules/ts-jest/node_modules/.bin/semver'
 WARN  Failed to create bin at /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/html-webpack-plugin@5.6.0_webpack@5.91.0_esbuild@0.20.2_webpack-cli@5.1.4_@webpack-cli+genera_o5z57zhf57uxuhfj26re5e4zha/node_modul
es/html-webpack-plugin/node_modules/.bin/webpack. ENOENT: no such file or directory, chmod '/home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/html-webpack-plugin@5.6.0_webpack@5.91.0_esbuild@0.20.2_webpack-cli@5.
1.4_@webpack-cli+genera_o5z57zhf57uxuhfj26re5e4zha/node_modules/html-webpack-plugin/node_modules/.bin/webpack'
 WARN  15 other warnings
Done in 1.8s
direnv: export +DETERMINISTIC_BUILD +PYTHONHASHSEED +PYTHONNOUSERSITE +PYTHONPATH +_PYTHON_HOST_PLATFORM +_PYTHON_SYSCONFIGDATA_NAME +buildPhase +dontAddDisableDepTrack +phases +preferLocalBuild +shellHook ~CONFIG_SHELL ~HOST
_PATH ~NIX_BINTOOLS ~NIX_CC ~NIX_CFLAGS_COMPILE ~NIX_LDFLAGS ~PATH ~XDG_DATA_DIRS ~buildInputs ~builder ~name ~nativeBuildInputs ~out ~shell ~stdenv

The files do seem to exist though, for example the first warning:

❯ ls -al /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/jest-config@29.7.0_@types+node@18.18.2_ts-node@10.9.2_@types+node@18.18.2_typescript@5.4.3_/node_modules/jest-config/node_modules/.bin/ts-node
.rwxr-xr-x 1.4k aa 18  6月 17:24  /home/aa/dev/talon/fidgetingbits-cursorless/node_modules/.pnpm/jest-config@29.7.0_@types+node@18.18.2_ts-node@10.9.2_@types+node@18.18.2_typescript@5.4.3_/node_modules/jest-config/node_modul
es/.bin/ts-node

Seems things mostly function (tested pnpm compile and pnpm build), so seems safe to ignore. Just noisy, so figured I'd mention in case anyone has ideas.

@pokey
Copy link
Member

pokey commented Jun 28, 2024

This PR is getting (quite) long in the tooth 😅 so I decided to try it out locally myself in preparation for merging it. I've never used nix before. Seems like it's working to me, so I'd be inclined to merge. Here's what I did from top to bottom:

➜ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
info: downloading installer https://install.determinate.systems/nix/tag/v0.19.0/nix-installer-aarch64-darwin
`nix-installer` needs to run as `root`, attempting to escalate now via `sudo`...
Password:
Nix install plan (v0.19.0)
Planner: macos (with default settings)

Planned actions:
* Create an encrypted APFS volume `Nix Store` for Nix on `disk3` and add it to `/etc/fstab` mounting on `/nix`
* Extract the bundled Nix (originally from /nix/store/xsmwdm9d3br9mhi14msbf346zfv7v56l-nix-binary-tarball-2.21.2/nix-2.21.2-aarch64-darwin.tar.xz)
* Create a directory tree in `/nix`
* Move the downloaded Nix into `/nix`
* Create build users (UID 301-332) and group (GID 30000)
* Configure Time Machine exclusions
* Setup the default Nix profile
* Place the Nix configuration in `/etc/nix/nix.conf`
* Configure the shell profiles
* Configuring zsh to support using Nix in non-interactive shells
* Create a `launchctl` plist to put Nix into your PATH
* Configure Nix daemon related settings with launchctl
* Remove directory `/nix/temp-install-dir`


Proceed? ([Y]es/[n]o/[e]xplain): 
 INFO Step: Create an encrypted APFS volume `Nix Store` for Nix on `disk3` and add it to `/etc/fstab` mounting on `/nix`
 INFO Step: Provision Nix
 INFO Step: Create build users (UID 301-332) and group (GID 30000)
 INFO Step: Configure Time Machine exclusions
 INFO Step: Configure Nix
 INFO Step: Configuring zsh to support using Nix in non-interactive shells
 INFO Step: Create a `launchctl` plist to put Nix into your PATH
 INFO Step: Configure Nix daemon related settings with launchctl
 INFO Step: Remove directory `/nix/temp-install-dir`
Nix was installed successfully!
To get started using Nix, open a new shell or run `. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh`. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh

➜ nix run "nixpkgs#hello"
Hello, world!

➜ nix build
error: flake 'git+file:///Users/pokey/src/cursorless' does not provide attribute 'packages.aarch64-darwin.default' or 'defaultPackage.aarch64-darwin'

➜ nix develop
bash: /Users/pokey/.rc: No such file or directory
pre-commit installed at .git/hooks/pre-commit
Scope: all 12 workspace projects
Lockfile is up to date, resolution step is skipped
Already up to date

   ╭─────────────────────────────────────────────────────────────────╮
   │                                                                 │
   │                Update available! 9.0.6 → 9.4.0.                 │
   │   Changelog: https://github.com/pnpm/pnpm/releases/tag/v9.4.0   │
   │     Run "corepack prepare pnpm@9.4.0 --activate" to update.     │
   │                                                                 │
   │     Follow @pnpmjs for updates: https://twitter.com/pnpmjs      │
   │                                                                 │
   ╰─────────────────────────────────────────────────────────────────╯

Done in 2.8s

(nix:nix-shell-env) pokey@MacBook-Pro-9:~/src/cursorless$ ls
CHANGELOG.md     bashbooks                           cursorless-talon      flake.lock    out                  pyproject.toml            tsconfig.json
CONTRIBUTING.md  bundle.vsix                         cursorless-talon-dev  flake.nix     package.json         queries                   typings
LICENSE          bundles                             cursorless.nvim       fonts         packages             schemas
NOTICE.md        changelog                           data                  images        patches              scripts
README.md        cursorless-development-0.26.0.vsix  dist                  netlify.toml  pnpm-lock.yaml       third-party-licenses.csv
SECURITY.md      cursorless-snippets                 docs                  node_modules  pnpm-workspace.yaml  tsconfig.base.json

(nix:nix-shell-env) pokey@MacBook-Pro-9:~/src/cursorless$ pnpm transform-recorded-tests

> cursorless@0.1.0 transform-recorded-tests /Users/pokey/src/cursorless
> ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts

You can see in that last step that I'm successfully running one of our scripts, so I think I'll declare victory.

@auscompgeek if there's any remaining improvements you think we should make, it would be awesome if you could file a quick follow-up issue. Thanks to everyone involved here!

@pokey pokey enabled auto-merge June 28, 2024 14:07
@pokey pokey added this pull request to the merge queue Jun 28, 2024
Merged via the queue into cursorless-dev:main with commit 35b1af8 Jun 28, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants