DavHau / nix-portable

Nix - Static, Permissionless, Installation-free, Pre-configured
MIT License
773 stars 29 forks source link

home-manager #66

Open zngguvnf opened 1 year ago

zngguvnf commented 1 year ago

Hi,

nix newbie here!

I often work on a centOS7 system on which I am not root. A few days ago I came across nix-portable and since then I can finally use modern software on the system. Thank you very much for this!

Currently I'm using a shell.nix file where I specify all my programms to install and use seperate dotfiles for configuration.

However the this really got me started, and I've now started setting up nixos in a VM with home-manager based on this minimal example config.

I wonder if there is any way to use the home-manager config also with nix-protable?

AmeerTaweel commented 7 months ago

This worked for me.

txomon commented 2 months ago

Nix newbie here too. @AmeerTaweel would you mind expanding on your answer? I have managed to install home-manager inside the nix-portable activated environment, however it won't allow me to do stuff like home-manager switch.

Supposing there is 0 prior knowledge and that a new non-root user has been given to you in a new machine where you don't have anything but your home.nix file, how would you end up with a home.nix that you switch to?

Thanks a lot for your pointers!

AmeerTaweel commented 2 months ago

I had a similar situation to @zngguvnf where I use a machine and I don't have root access. I tried to get Nix working there for a while. But I gave up eventually but I don't remember exactly what was wrong. This repo received a bunch of updates since then, maybe it will work if I try it now, but I don't use that machine anymore.

However, I can try to help you with your issue. Do you mind sharing home-manager switch output? Maybe we can figure out why it fails.

DavHau commented 2 months ago

Hm, guess once I find some time I should do a home-manager nix-portable guide

AmeerTaweel commented 2 months ago

I'd appreciate that for sure

ccornix commented 2 months ago

I managed to activate my Home Manager config and create a working environment on Debian 12 as described below.

I downloaded nix-portable into ~/.local/bin and entered the following Nix shell to switch to my HM config:

NP_RUNTIME=bwrap ~/local/.bin/nix-portable nix shell nixpkgs#{bashInteractive,nix}
nix run github:nix-community/home-manager -- switch -b old --flake <my-hm-flake-uri>

To easily enter the new HM environment, I created a script ~/.local/bin/hm-env:

#!/usr/bin/env bash
NP_RUNTIME=bwrap $HOME/.local/bin/nix-portable nix run nixpkgs#bashInteractive --offline

This way, I can SSH directly into the HM env as

ssh -t <address> .local/bin/hm-env

Further remarks:

txomon commented 2 months ago

Thank you so much @ccornix

So if I understand the solution, it relies on a double user-namespace. First we use nix-portable with bwrap to atain the mapping from ~/.nix-portable/nix to /nix, and then home-manager to use nix normally using real user-namespaces.

I might be a purist here, but I was expecting to take advantage of nix's --store flag together with home manager to avoid having to use nix-portable's wrapping method :/

Given that we are speaking about nix here and this should be easily reproducible, I will start working with @ccornix 's solution, and hope for whenever @DavHau has time to make the command that avoid the double wrap (if possible).

ccornix commented 2 months ago

Thank you so much @ccornix

So if I understand the solution, it relies on a double user-namespace. First we use nix-portable with bwrap to atain the mapping from ~/.nix-portable/nix to /nix, and then home-manager to use nix normally using real user-namespaces.

I might be a purist here, but I was expecting to take advantage of nix's --store flag together with home manager to avoid having to use nix-portable's wrapping method :/

Given that we are speaking about nix here and this should be easily reproducible, I will start working with @ccornix 's solution, and hope for whenever @DavHau has time to make the command that avoid the double wrap (if possible).

You might want to subscribe to #98 then ^^

txomon commented 2 months ago

Just in case, if anyone comes and tries to have a starter pack, with all the things we discussed in a single script that I found works pretty good, find it below.

There are a few things to take into account:

  1. In this script, because nix I understand is ok to come from nix-portable, I am adding symlinks to the path
  2. You can't manage bash with home-manager because you will get inside an initialization chicken-and-egg problem between .bashrc with the proper config being there and the symlink to the profile being there. I don't use bash as my shell, and only use it to launch my shell
  3. This will auto-activate on login
  4. You can tune the home-manager switch line so that it directly populates your thing, in which case, just remember @ccornix's message on making sure you have set home.sessionVariables.PATH = "$HOME/.nix-profile/bin:$PATH";
#!/bin/bash

set -ex
export PATH=$PATH:$HOME/.local/bin
mkdir -p .local/bin
cd .local/bin

# Download nix-portable
curl -L "https://github.com/DavHau/nix-portable/releases/latest/download/nix-portable-$(uname -m)" > ./nix-portable

# Generate symlinks for seamless integration
chmod +x nix-portable
ln -s nix-portable nix
ln -s nix-portable nix-channel
ln -s nix-portable nix-copy-closure
ln -s nix-portable nix-env
ln -s nix-portable nix-instantiate
ln -s nix-portable nix-prefetch-url
ln -s nix-portable nix-store
ln -s nix-portable nix-build
ln -s nix-portable nix-collect-garbage
ln -s nix-portable nix-daemon
ln -s nix-portable nix-hash
ln -s nix-portable nix-shell

cd ~

# Init home-manager
NP_RUNTIME=bwrap nix-portable nix shell nixpkgs#{bashInteractive,nix} <<EOF
nix run github:nix-community/home-manager -- init
EOF

# Add home-manager to its own path
echo 'Add the following in your home.nix file: `home.sessionVariables.PATH = "$HOME/.nix-profile/bin:$PATH";`'
sed -i '/home.sessionVariables = {/a\    PATH = "$HOME/.nix-profile/bin:$PATH";' ~/.config/home-manager/home.nix

# home manager switch
NP_RUNTIME=bwrap nix-portable nix shell nixpkgs#{bashInteractive,nix} <<EOF
nix run github:nix-community/home-manager -- switch
EOF

# Make new sessions use the shell automatically
cat >~/.bashrc <<EOF
export PATH=\$PATH:\$HOME/.local/bin

if [ -z "\$__NIX_PORTABLE_ACTIVATED" ]; then
        export __NIX_PORTABLE_ACTIVATED=1
        NP_RUNTIME=bwrap nix-portable nix run nixpkgs#bashInteractive --offline
        exit
else
        . \$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh
fi

# If not running interactively, don't do anything
[[ \$- != *i* ]] && return

# Set something for the cmd line
PS1='[\u@\h \W]\\\$ '
EOF

echo 'Please remember to relogin so that the environment gets activated'