Skip to content

Instantly share code, notes, and snippets.

Create a Debian rootfs as a non-root user using unshare --user --map-root-user and debootstrap

This short article explains a simple, practical one-shot workflow that lets a non-root user create a root filesystem (rootfs) with debootstrap and install packages inside it using apt, by running the operations inside an unprivileged user namespace where the user appears as root. It includes a ready-to-run script, key explanations, and troubleshooting hints.


What this does (short)

Run the provided script as a regular user. It uses unshare --user --map-root-user to create a user/mount/pid namespace where your UID is mapped to UID 0 inside that namespace. Inside that namespace the script mounts necessary pseudo-filesystems, runs debootstrap to populate a target directory with a minimal Debian rootfs, then chroots into that rootfs and runs apt-get to install packages (e.g. vim). When finished, it unmounts the bind mounts and leaves the populated rootfs at the path you chose.

Bootstrap a Rocky root — no root required (uses unshare + dnf)

The snippet below bootstraps a minimal Rocky Linux root filesystem at "$HOME/path/to/rocky/root" while making it explicit that you do not need real root privileges on the host. It relies on Linux user namespaces so the process is mapped to root inside the namespace but runs as your unprivileged user on the host.

TARGET="$HOME/path/to/rocky/root"

# Create new userns / mount ns / pid ns and run the command inside (no real root required)
unshare --user --map-root-user --mount --pid --fork \
 dnf -y --installroot="$TARGET" \

Creating the same Windows .exe CLI launchers that pip makes — using distlib

When you pip install a package that exposes console entry points on Windows, pip (via its bundled distlib) produces small native .exe launcher files in the Scripts (or Scripts\) directory. Those .exe files are merely launchers that invoke the target Python interpreter and run your package’s entry point. You can reproduce that same functionality directly from Python by calling distlib.scripts.ScriptMaker — in other words, the .exe-creation logic is available as a callable library.

Below is a compact guide and example code you can drop into a script to generate the same Windows EXE wrappers pip would create.


Quick summary

Lightweight Namespace Shell: unshare --user --map-root-user --mount --pid --fork bash

unshare --user --map-root-user --mount --pid --fork bash is a compact one-liner that starts a new interactive shell with several Linux namespaces isolated. It’s a handy way to get a “root” shell that’s isolated from the rest of the system — useful for experimenting, testing, and lightweight container-like isolation without full container tooling.

The command

unshare --user --map-root-user --mount --pid --fork bash

Quick guide: mac.yml — Open a macOS tmate debug session in GitHub Actions

This tiny GitHub Actions workflow lets you start an interactive remote debugging session on a macOS runner using tmate. It’s handy when a job fails only on macOS and you want to hop into the running environment to inspect files, processes, or reproduce the problem live.

What the workflow does

name: Mac Debug (tmate)
on: workflow_dispatch           # manual trigger
jobs:

Make a CLI command from your Python package (quick guide)

In short: declare a console_scripts entry point in your package metadata, activate the target virtual environment, then run pip install (during development use pip install -e .). That will place an executable wrapper in ./venv/bin (Unix/macOS) or venv\Scripts (Windows). Below are concrete examples and step-by-step instructions.


1) Example project layout

myproject/

A Simple Tool for Downloading Files from Hugging Face

Developers who work with machine learning models often need to download files from Hugging Face repositories. While the Hugging Face website provides links, manually handling URLs and paths can be inconvenient. To make this process easier, the script hf_get_from_url.py was created.

What the Script Does

The script helps users fetch files directly from Hugging Face by interpreting different types of inputs. It can handle:

  • Full Hugging Face URLs with blob or resolve.
  • Shortened forms like huggingface.co/owner/repo/blob/main/file.

How to Squash Past Commits with Git Rebase

Squashing commits with Git’s rebase command is useful when you want to clean up your commit history. Here’s a step-by-step guide.


1. Check Which Commits to Squash

First, review the commit log to decide which commits you want to combine:

Creating a Git Branch from HEAD^

When working with Git, sometimes you may want to create a new branch based on the commit before the current HEAD. In Git notation, HEAD^ refers to the parent of the current commit.

Command

To create a branch from HEAD^, run:

git checkout -b  HEAD^

Tracing Function Calls in C++ with GDB and Python

When debugging recursive functions in C++, it can be helpful to trace every entry and exit of a function. With GDB (GNU Debugger) and a custom Python script, we can automate this process.


Step 1: Compile with Debug Information

We first compile our program with debugging symbols enabled and without optimization: