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

home-manager #66

Open
zngguvnf opened this issue Jun 3, 2023 · 9 comments
Open

home-manager #66

zngguvnf opened this issue Jun 3, 2023 · 9 comments

Comments

@zngguvnf
Copy link

zngguvnf commented Jun 3, 2023

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
Copy link

This worked for me.

@txomon
Copy link

txomon commented Apr 16, 2024

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
Copy link

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
Copy link
Owner

DavHau commented Apr 17, 2024

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

@AmeerTaweel
Copy link

I'd appreciate that for sure

@ccornix
Copy link

ccornix commented Apr 17, 2024

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:

  • This solution also required some polishing of the HM config; notably, I needed to add
home.sessionVariables.PATH = "$HOME/.nix-profile/bin:$PATH";

to access all the commands from HM-installed packages. Note that home.sessionPath would have appended to PATH but I wanted my HM-installed packages take precedence over the native ones.

  • Without forcing NP_RUNTIME=bwrap, I got the error message
error: setting up a private mount namespace: Operation not permitted

I assume nix-portable automatically selected the nix --store mechanism and creating a nested mount namespace was forbidden at some point.

@txomon
Copy link

txomon commented Apr 18, 2024

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
Copy link

ccornix commented Apr 19, 2024

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
Copy link

txomon commented Apr 20, 2024

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'

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

No branches or pull requests

5 participants