Using files to Install packages for system and personal use

I have been using NixOS for a little while now, mostly as a package manager and as something like Python’s VirtualEnv on steroids (albeit without using it to its full potential). For several years, I have wished I could keep my favorite packages in a file and install them whenever I have to install a fresh system, but also have them removed when I remove them from the file, but in using Debian, I wasn’t able to do this in a practical manner.I have attempted to do this using NixOS, but I have had mixed results.

On my MacOS X system, I have a default.nix that looks something like this:

  packageOverrides = pkgs: with pkgs; {
    myProfile = writeText "my-profile" ''
    export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
    export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
    export INFOPATH=$HOME/.nix-profile/share/info:/nix/var/nix/profiles/default/share/info:/usr/share/info

    myPackages = pkgs.buildEnv {
      name = "my-packages";
      paths = [
        (runCommand "profile" {} ''
          mkdir -p $out/etc/profile.d
          cp ${myProfile} $out/etc/profile.d/
      pathsToLink = [ "/share/man" "/share/doc" "/bin" ];
      extraOutputsToInstall = [ "man" "doc" ];

On my Mac OS X, when I run nix-env -f default.nix -i --remove-all, NixOS will install and remove files as I wish.

Now that I’ve tried to do the same thing on my Kubuntu machine at home, however, I have run into two problems.

First, if I try running this as root (so I could install/remove files for the entire system), this seems to remove everything but the packages I have selected. I had to remove NixOS and start again when I did this.

Second, if I try running this as a user (so I could install some packages for my own personal use – and install other packages for my wife, that I might not want, for when she needs to use my computer in a pinch), all it seems to do is create a derivation, but not install anything.

I have tried several resources to get this to work, but so far, the resource I’ve had the most success with has been NixOS - Nixpkgs 21.05 manual.

This is something that I can’t help but think ought to be relatively simple, so I can’t help but wonder: what am I missing?