this post was submitted on 26 Feb 2024
20 points (100.0% liked)

Nix / NixOS

1782 readers
1 users here now

Main links

Videos

founded 1 year ago
MODERATORS
 

I just switched one of my systems over to NixOS from Arch and so far it seems interesting. One question I had is regarding the nix-shell. So I get the basic concept of it and that it allows creating a shell that has packages installed with that shell making ideal for dev environments. I've even seen talks where the suggest nix-shells over docker/podman, my question is how is persistent data (like databases) handled?

top 13 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 9 months ago

Nix shell basically just downloads the software (if it's not already downloaded) and then modifies your PATH to include the new software.

Persistent data stay persistent.

Nix shell has one use case common with docker (local development), but other than that the solutions are not similar at all.

[–] [email protected] 6 points 9 months ago

Services like databases that manage runtime information usually store that information in /var or your home directory as usual.

[–] [email protected] 4 points 9 months ago (1 children)

In general Nix doesn't manage persistent data. It will be stored in whatever location the tool writes it to.

nix-shell is a very simple program. Basically it downloads/builds the package you have configured, then sets up environment variables like PATH to include that package. It would be similar to downloading a pre-built tarball run running ./foo inside of that.

For most built-in modules this will be /var/lib/foo for a service foo. If you run nix-shell the service will write data wherever it would normally write data on any other distribution.

[–] [email protected] 2 points 9 months ago (1 children)

Okay, that's good to know. I just wanted to make sure that if I had something like MariaDB or Postgres get installed with nix-shell that I wouldn't lose everything thus having to set everything back up next time I start the shell again.

[–] [email protected] 3 points 9 months ago

No, you won't lose anything. However do be aware that unless you specifically configure different data directories using the same DBMS in multiple projects will use the same underlying data files.

Typically this won't be a problem, and you can just create different logical databases if necessary.

[–] [email protected] 1 points 8 months ago (1 children)

Okay, hopefully someone here can point out where I am going wrong with setting up my dev shell the way I want. I am currently using direnv and nix-direnv to manage the dev shell, and I found this blog post which shows a way to start and stop MySQL/Mariadb but I am having some weird issues with it.

Currently my flake looks like this:

{
  description = "A basic flake with a shell";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { nixpkgs, flake-utils, ... }@inputs:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        devShells.default = pkgs.mkShell {
          packages = with pkgs; [
            bashInteractive
            php
          ];

          buildInputs = [ pkgs.mariadb ];

          shellHook = ''
          export MYSQL_BASEDIR=${pkgs.mariadb}
          export MYSQL_HOME=$PWD/.direnv/mysql
          export MYSQL_DATADIR=$MYSQL_HOME/data
          export MYSQL_UNIX_PORT=$MYSQL_HOME/mysql.sock
          export MYSQL_PID_FILE=$MYSQL_HOME/mysql.pid
          alias mysql='mysql -u root'

          if [[ ! -d $MYSQL_HOME ]]; then
            mariadb-install-db --auth-root-authentication-method=normal \
              --datadir="$MYSQL_DATADIR" --basedir="$MYSQL_BASEDIR" \
              --pid-file="$MYSQL_PID_FILE"
          fi

          mariadbd --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE \
            --socket=$MYSQL_UNIX_PORT --tmpdir='/tmp' 2>/dev/null &
          MYSQL_PID=$!
          '';
        };
      });
}

When I run it like this mariadbd starts just fine, but doesn't get backgrounded dispite the & making that shell session useless which is not what I want as I have to spawn a second shell just to do anything.

Even weirder is when I add the finish() function and the call to trap like in the blog post then mariadbd doesn't start (or starts and immedently gets killed).

[–] onlinepersona -1 points 4 months ago

mariadb doesn't get backgrounded? Are you sure? Are you able to type commands and see their output? Also, what happens when putting set -x at the start of your shell hook? You should see the last line printed e.g MYSQL_PID=12345

Anti Commercial-AI license

[–] [email protected] 1 points 9 months ago* (last edited 9 months ago) (1 children)

I just heard about NixOS today and decided to blow away my ubuntu that was laggy and try it. So far i like what i see, but i can tell i have some learning to do. I really need to learn to compile from source and how to fix build errors. Theres a package ubuntu has that only comes in a .deb or source.

https://github.com/haveno-dex/haveno

https://github.com/haveno-dex/haveno/blob/master/docs/installing.md

[–] [email protected] 2 points 9 months ago (3 children)

How do you use your computer? Did you really just blow away your OS and jump to one you just heard about? I ask because I spend many months preparing to make the move, and I'm still working at a deficit, since my 4 year old OS had so many hours of tinkering that went undocumented and forgotten. Im still slowly configuring my nixos box. How did you use your computer?

[–] [email protected] 2 points 8 months ago

I backed up my 2 year old Debian 2 weeks ago and burned it down for NixOs. With the exception of getting Nvidia prime straight and having to learn configs multiple times as I did the start with flakes or home manager, It's been incredibly smooth sailing.

It took me two or three days on Debian to get OBS and plugins straightened out. Nix worked with just a couple lines of config. I had Nix back to where I left off with Debian after approximately 2 days.

Being able to see other people's configs and dork around with what other people are playing with without screwing up my system we're going through dependency hell is nothing short of amazing.

[–] [email protected] 1 points 9 months ago (1 children)

Well, part of the point of NixOS is to eliminate that whole issue of forgotten tinkering -- the whole system is defined right there in a few modules, or even one file, and there's no way for un-tracked tinkering to exist outside those files.

But can I ask how you use your computer? What goes into these months of prep? I really can't imagine it.

[–] [email protected] 1 points 8 months ago* (last edited 8 months ago)

My old OS is kind of a blackbox, in the that I played with countless tools, and I'm not really sure what I will want to reference later. For instance

  • Configure different wine tweaks for games
  • Playing with Monero and crypto wallets/miners
  • Different VMs for tinkering with OSs
  • Self-hosted software in docker containers, plus volumes
  • All the software tools I used for projects
  • Video game modding: some in linux, some in the "windows filesystem" created by steam with proton
  • Software installed from programming package managers: pip, cargo, npm

...and on. I played with a lot of things without regard for longevity or preservation. I didn't even takes notes on what I did most of the time. So I got very worried about just switching OSs without a plan in place. Ultimately, I ended up doing the following to transition.

  1. Started adding flakes to all my development projects. This would let me get my environment well defined before messing with my installations.
  2. Installed nix and home-manager, and started slowly uninstalling packages from cargo/pip/apt/npm/flatpak/appimage/snap and add them to my nix config.
  3. Bought a second ssd so I could preserve the current OS as is (this was much easier than shrinking partitions and install nixos alongside it, but it could have been done)
  4. Finally made the jump, using the NixOS configuration on my laptop as a jumping off point. And I still reference my old OS as is, booting into once in a while to remember where things were. I don't know if I'll ever actually wipe the drive. I'm just not sure.

But of course...I'm on NixOS now! So much of these configurations and lists of software packages, will be documented forever.

[–] [email protected] 0 points 9 months ago

Honestly, I barely use it at all these days. I do most of my computing from my phone, so would be very hesitant to just blow that away on a whim like that. But on the desktop where I don't do much, it's fine.