7

I made a post a while ago asking what you do when NixOS isn't cutting it. You need a package that isn't available as a flatpak/appimage or already in nixpkgs. You don't want to build from source, because it's either too difficult or too time consuming. One suggestion was containerization or virtual machines, but those seemed too cumbersome. Well, distrobox is the tool that fixes it.

Distrobox is a shell script that wraps over docker/podman to run a container of a distribution of your choice. But it does it behind a very high level API, and integrates the container environment seemlessly with your host environment. It is seriously as easy as this, if you need to install something with apt inside debian.

$ distrobox create -n my_debian --image debian:latest
$ distrobox enter my_debian

And bang, your in a debian container and it won't even feel like it. It automatically integrates your shell environment and maps your root directory inside the container (or something like that.) You seriously wouldn't know unless you neofetch. Best part is that since everything is in the nix store, every program in your environment should work, for the most part, inside this container. I've not noticed problems yet.

Tada! apt is available in this environment and you can install what you need. Then you can run it while inside the container. From the host machine, outside the container, you can run it directly too. Say you installed program X in debian:

$ distrobox enter my_debian -- X

And it will just run the command and send you back to the host machine.

In the case of docker, you can type docker ps and it will show you your debian image my_debian listed.

There's two more things I want to do to really polish this workflow. The first is to change my shell prompt so I know that I'm actually in debian without typing neofetch! Inside the box the variable CONTAINER_ID is set and the hostname is modified. I've adjusted my starship prompt to look like this when inside the box:

distrobox:my_debian ~ $

And lastly, I really want to blur the lines. If I install X in debian, I want to just call it directly from the host as X, not invoke my debian instance with distrobox enter.

When you type X and the program is missing, bash (and fish and zsh I'm sure) runs a hook that you can look at by typing

$ declare -p -f command_not_found_handle

By overriding this, you could first have it try the inside container if it can't find the application in the host container, like so.

command_not_found_handle () {
  distrobox enter my_debian -- $@
}

This is not a perfect solution, but I'm still experimenting with how to integrate this both seamlessly and also not accidentally run things inside debian and not realize it. If you have suggestions for how to improve handling calling commands from the outside environment, please share. Best case might just be adding aliases for programs explicitly. For example, `alias X=distrobox enter my_debian -- X.

Anyway, distrobox is the solution! This is one more barrier removed that was preventing me from moving my main computer over to NixOS. I'm so happy to have found this and wanted to share.

top 6 comments
sorted by: hot top controversial new old
[-] hallettj@beehaw.org 2 points 1 year ago

It looks like a useful tool - thanks for sharing all of that info! Personally I would prefer to set up a shell alias or wrapper script for each command I want to run in distrobox instead of using a missing-program hook.

[-] rutrum@lm.paradisus.day 2 points 1 year ago

I think thats the best way too, and it doing that ends up cateloguing all my immutable packages in one place.

[-] pol@infosec.pub 2 points 1 year ago

When a software is not available(very rare), I take a bit of time to learn how to package it and then I propose it to the main nixpkgs repository (check https://github.com/nix-community/nix-init). So I can enjoy learning new things and potentially help some other users...and usually updates are provided automatically by rryan-tm bot!

I understand that distrobox might help in short terms, but this is not the solution in the end.

[-] rutrum@lm.paradisus.day 3 points 1 year ago

This need came from a project I've been working on, to wrap the wasm4 retro console in a flake https://github.com/rutrum/wasm4-nix . I've been having an absolute bear of a time, weeks of work without success. Without knowing I had an option to install it easily, it has prevented me from actually using the software for other projects. I agree, this solution is really intended for that particular 1% of the time.

[-] tengkuizdihar@programming.dev 1 points 3 months ago

Does it connect to my GPU?

[-] rutrum@lm.paradisus.day 2 points 3 months ago

According the docs, support for intel and amd graphics is supported out of the box. For nvidia, you can pass --nvidia to the distrobox create command and enable functionality. So yes it does!

this post was submitted on 05 Nov 2023
7 points (100.0% liked)

NixOS

1016 readers
1 users here now

NixOS is a Linux distribution built on top of the Nix package manager. Its declarative configuration allows reliable system upgrades via several official channels of stability and size.

This community discusses NixOS, Nix, and everything related.

founded 2 years ago
MODERATORS