thoughtpolice / buck2-nix

Do not taunt happy fun ball
59 stars 4 forks source link

improving automated setup #15

Closed dezren39 closed 1 year ago

dezren39 commented 1 year ago

here are some notes from installing, i have gotten as far as nix run completing and then running cd, but then i dont have systemd in wsl. if i throw this in a vm or container or something it probably would do systemd right and get to working on ubuntu?

final script essentially:

cd ~
sudo mkdir /nix
CURRENT_USER="$USER"
sudo chown -R "$CURRENT_USER:$CURRENT_USER" /nix
sudo chmod 755 /nix
sudo mkdir -p /etc/nix
sudo touch /etc/nix/nix.conf
sudo chown -R "$CURRENT_USER:$CURRENT_USER" /etc/nix
sudo chmod 755 /etc/nix
sudo chmod 644 /etc/nix/nix.conf
echo 'sandbox = false' | tee -a /etc/nix/nix.conf
echo 'use-sqlite-wal = false' | tee -a /etc/nix/nix.conf
echo "trusted-users = $CURRENT_USER" | tee -a /etc/nix/nix.conf
echo 'experimental-features = flakes nix-command ca-derivations' | sudo tee -a /etc/nix/nix.conf
sh <(curl -L https://nixos.org/nix/install) --no-daemon
source "$HOME/.nix-profile/etc/profile.d/nix.sh"
nix-env -i direnv
echo "eval \"\$(direnv hook bash)\"" >> ~/.profile
eval "$(direnv hook bash)"
# manual user input: [ y, {enter}*3, y*2, isWSL ? n : y ]
nix run \
    --tarball-ttl 0 \
    --accept-flake-config \
    'github:thoughtpolice/buck2-nix?dir=buck/nix#setup'
cd ./buck2-nix.sl

full script notes of first tries

#!/bin/bash

# Host: Windows 10 Enterprise (Developer mode enabled)
# WSL: Ubuntu 20.04 LTS
# Date: 2023-04-18
# Both WSL and Windows are relatively up-to-date, dirty, development environments with a bunch of working build tools.
# I'm not sure if this is a good idea, but I'm going to try to install nix on top of this.
# Also reran with fresh ubuntu wsl install.
cd ~ || return
pwd

# the chown/chmod stuff probably could just stay root, idk how nix works
sudo mkdir /nix
CURRENT_USER="$(whoami)"
sudo chown -R "$CURRENT_USER:$CURRENT_USER" /nix
sudo chmod 755 /nix

sudo mkdir -p /etc/nix
sudo touch /etc/nix/nix.conf
sudo chown -R "$CURRENT_USER:$CURRENT_USER" /etc/nix
sudo chmod 755 /etc/nix
sudo chmod 644 /etc/nix/nix.conf
# based on this for ubuntu wsl: https://ariya.io/2020/05/nix-package-manager-on-ubuntu-or-debian
# probably would be nice to have a nixos config for this idk how that works though
# i wonder do you make docker containers using nix? is that a thing?
# nix.conf can also be in ~/.config/nix/nix.conf ? don't know that trusted-users works for that
echo 'sandbox = false' | tee -a /etc/nix/nix.conf
echo 'use-sqlite-wal = false' | tee -a /etc/nix/nix.conf
echo "trusted-users = $CURRENT_USER" | tee -a /etc/nix/nix.conf
# flakes nix-command needed for `nix run`
# ca-derivations needed for `cd buck2-nix.sl`
echo 'experimental-features = flakes nix-command ca-derivations' | sudo tee -a /etc/nix/nix.conf

sh <(curl -L https://nixos.org/nix/install) --no-daemon

. /home/user/.nix-profile/etc/profile.d/nix.sh

# Error:
#    0: direnv not installed
# Location:
#    src/main.rs:260
#   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SPANTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#    0: setup::check_direnv
#       at src/main.rs:243
# // XXX FIXME (aseipp): implement direnv installation
# nix-env -i direnv
# sudo rm -f "/home/linuxbrew/.linuxbrew/bin/direnv"
# sudo rm -f "/home/$CURRENT_USER/.nix-profile/bin/direnv"
# sudo apt install -y direnv
# eval "$(direnv hook bash)"
# source ~/.profile
# based on this, nix should be able to do this install: https://gist.github.com/FruitieX/28500ee32e00a5687c7f5031fb5b9922
# nix-env -iA direnv
# echo "eval \"\$(direnv hook bash)\"" >> ~/.profile
# source ~/.profile

# ultimately i installed and uninstalled multiple times
# i tried exiting and re-entering the shell
# eventually, after a full clean and deleting any which direnv -- sudo apt worked.
# sudo apt install -y direnv

# while apt install works, upon cd into buck2-nix.sl,
# errors on a version check,
# direnv from ubuntu is 2.21.2, req 2.21.3
# so i installed direnv from source, but this is also 2.21.2
# it seems to behave differently, but i'm not sure why
# success like:
# user@PF3EHGYL:~$ cd buck2-nix.sl/
# direnv: loading ~/buck2-nix.sl/.envrc
# direnv: using flake ./buck/nix --accept-flake-config
# evaluating derivation 'path:/home/user/buck2-nix.sl/buck/nix#devShells.x86_64-linux.default'direnv: ([/home/linuxbrew/.linuxbrew/bin/direnv export bash]) is taking a while to execute. Use CTRL-C to give up.
# appears to redownload similar to first install run, may be because of direnv uninstall/install cycles
# curl -sfL https://direnv.net/install.sh | bash

nix-env -i hello
which hello
hello

# maybe this will work, or else exit and re-enter idk
echo "eval \"\$(direnv hook bash)\"" >> ~/.profile
eval "$(direnv hook bash)"

# ~2.9+0.6 GiB
# used defaults when prompted: sapling, current directory, buck2-nix.sl, etc, {enter 3}{y 2}
nix run \
    --tarball-ttl 0 \
    --accept-flake-config \
    'github:thoughtpolice/buck2-nix?dir=buck/nix#setup'

if [ -d buck2-nix.sl ]; then
  # ~2.9+0.6 GiB again?
  cd ./buck2-nix.sl || return
  # around here i had issues with ca-derivations and trusted-user, fixed with sys conf changes

  # had an issue with query cache -- DISK FULL OOPS
  # [1/361/368 built, 1028 copied (2888.4/2892.3 MiB), 600.7 MiB DL] building buck2-unstable-2023-04-17 (buildPhase): error: unable to delete old query cache at `/tmp/nix-build-buck2-unstable-2023-04-17.drv-0/source/target/x86_64-unknown-linux-gnu/release/incrementa
  # [1/361/368 built, 1028 copied (2888.4/2892.3 MiB), 600.7 MiB DL] building buck2-unstable-2023-04-17 (buildPhase): error: could not compile `buck2_action_impl` due to previous error
  # on ctrl+c at this point
  # error (ignored): error: cannot unlink '/tmp/nix-build-buck2-unstable-2023-04-17.drv-0/.cargo/config': Read-only file system
  # error (ignored): error: writing to file: Read-only file system
  # error: interrupted by the user
  # direnv: error signal: killed
  # user@PF3EHGYL:~/buck2-nix.sl$ cd ..
  # direnv: unloading
  # user@PF3EHGYL:~$ cd buck2-nix.sl/
  # direnv: loading ~/buck2-nix.sl/.envrc
  # error: could not set permissions on '/nix/var/nix/profiles/per-user' to 755: Read-only file system
  # error: could not set permissions on '/nix/var/nix/profiles/per-user' to 755: Read-only file system
  # direnv: using flake ./buck/nix --accept-flake-config
  # direnv: nix-direnv: nix version is older than the required 2.4.
  # direnv: error exit status 1
  # user@PF3EHGYL:~/buck2-nix.sl$ nix --version
  # nix (Nix) 2.15.0
  # user@PF3EHGYL:~/buck2-nix.sl$ nix-direnv --version
  # nix-direnv: command not found
  # disk was full! deleting files trying again.
  # this caused major issues for my ubuntu wsl,
  # wsl --terminate ubuntu
  # even after this `ubuntu` wouldn't connect.
  # wsl --unregister ubuntu
  # wsl --install -d ubuntu
  # wsl --set-version ubuntu 2
  # ^^ probably default, but just in case
  # ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--24vhlhl99pvzg32d50yvyqrw69w9c0r8--source': No such file or directory
  # direnv: nix-direnv: renewed cache
  # direnv: watchman: ERROR: cannot enable, because you aren't using systemd
  # direnv: error exit status 1

    pwd
fi
dezren39 commented 1 year ago

i tried disabling watchman because of systemd, but i'm still seeing some ln errors. maybe the rest still worked? # manual user input: accept defaults [ y, {enter}*3, y*2, n ]

user@PF3EHGYL:~$ cd buck2-nix.sl/
direnv: loading ~/buck2-nix.sl/.envrc
direnv: using flake ./buck/nix --accept-flake-config
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--profile--121fb47f0d42ebce46b17b5df030e132bcaba6b8': No such file or directory
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--4w4b4mxfhdc9lf3rsf4qqa9z8xnijbxl--source': No such file or directory
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--mcqspn5p5dy263y3nfb0dfzi7inms7j0--source': No such file or directory
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--9hi5a1jrhqsgah4ma9asmyia0105drp5--source': No such file or directory
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--yj1wxm9hh8610iyzqnz75kvs6xl8j3my--source': No such file or directory
ln: failed to create symbolic link '/nix/var/nix/gcroots/per-user/user/home-user-buck2--nix.sl-.direnv-flake--inputs--24vhlhl99pvzg32d50yvyqrw69w9c0r8--source': No such file or directory
direnv: nix-direnv: renewed cache
direnv: watchman: not enabled, because .use_watchman is missing, and we're not in a CI build
direnv: export +AR +AS +BUCK2_TEST_DISABLE_LOG_UPLOAD +CC +CONFIG_SHELL +CXX +DETERMINISTIC_BUILD +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +OBJCOPY +OBJDUMP +PYTHONHASHSEED +PYTHONNOUSERSITE +PYTHONPATH +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +_PYTHON_HOST_PLATFORM +_PYTHON_SYSCONFIGDATA_NAME +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS

when inside, buck2 build starts and completes

user@PF3EHGYL:~/buck2-nix.sl$ buck2 build ///...
watchman fresh instance event, clearing cache
Build ID: 06bc7f4c-a94c-4a5d-8c29-8be29a90a0fc
Jobs completed: 159. Time elapsed: 117.3s. Cache hits: 0%. Commands: 28 (cached: 0, remote: 0, local: 28)
BUILD SUCCEEDED

my ln issues might wsl or those chmod/chown calls i had maybe

thoughtpolice commented 1 year ago

Yeah, if you use single user mode that should work to some degree. The single-user-mode (--no-daemon) installer should have taken care of most of that setup stuff for you, though?

Really, the installer should be turned into a static executable that's produced, and it should use https://github.com/DeterminateSystems/nix-installer/ as a library to install Nix too on top of setting up buck and the repository, frankly. But the Nix installer worked out of the box for me on Windows 11, Ubuntu 22.04, systemd=true in /etc/wsl.conf, in daemon/multi-user mode.

Apparently systemd in WSL2 is apparently only supported on Windows 11. I'm not super enthusiastic about taking reports about single-user mode Nix if you have problems, because it's often busted as hell — there is no guarantee any arbitrary .nix file will actually work when you build it without the sandbox, it could just break — Nix expressions are often written with the benefit of the sandbox purifying the environment and aren't tested otherwise. But you can try enabling sandbox = true and see if your WSL2 kernel supports enough cgroup stuff to work in single-user mode. The daemon is really a requirement for robust builds, though.

The ln stuff is probably because direnv expects the Nix installation to support per-user GC links in that directory, but it probably doesn't install that properly in single user mode.

That said, I'm glad it at least built for you! That's something...

dezren39 commented 1 year ago

Thanks! I'll look into using systemd with nix-installer and try again on one of my w11 boxes.

dezren39 commented 1 year ago

awesome! systemd works with windows 10 too now, just need to reinstall with windows store and you'll get the latest updates.

i tried nix-installer and it worked great but it installed like .14 and the scripts required .15 so i used the regular nix installer except without --no-daemon, and now that systemd is enabled i now have a working multiuser nix in both win10 and win11. after that, the direnv install worked fine and so did the nix run and the direnv load on directory change.

i'll close this ticket for now. it looks like setup is basically 'run nix installer, nix-env -i direnv, nix run ..., cd ...' which is very easy