Wrapped firefox/librewolf setup

I have been working on a LibreWolf wrap after I moved away from home-manager. The most annoying thing about configuring browsers, at least for me, has been that they are usually tied to the home and even if you get past that, the configuration drifts with state accumulating over time.

Impermanence helps a little at least with the state, but that forces you to have an impermanent setup and still the configuration is tied to home, not allowing you to run multiple instances of different configurations at the same time. Wrapping with pkgs.wrapFirefox does get you quite far but it doesn’t allow for example user chromes (At least as far as I know).

My approach for solving these problems is to generate a tmp profile in XDG_RUNTIME_DIR on LibreWolf launch and copy the configuration details to it and also the cookies. And when you close the browser everything gets deleted and cookies saved.

Having a tmp profile for each browser instance even when they shared the same configuration was a little too wasteful and annoying for me, so each configuration checks XDG_RUNTIME_DIR/librewolf-$hash existence and if there already is running a configuration it just launches the browser with that profile directory. This way state accumulation is kept to a minimum, but not limited too much.

Any ideas on improving this setup/opinions, here is the setup:

        pkgs.symlinkJoin {
          name = "librewolf";
          paths = [librewolfWrapped];
          postBuild = ''
            mv $out/bin/librewolf $out/bin/.librewolf

            cat > $out/bin/librewolf << EOF
            #!${pkgs.runtimeShell}
            export FONTCONFIG_FILE=${fontsConf}

            state="\$HOME/.librewolf"
            mkdir -p "\$state"

            hash=$(basename "$out" | cut -d- -f1)

            base="\$XDG_RUNTIME_DIR"
            [ -n "\$base" ] || base=/tmp
            dir="\$base/librewolf-\$hash"

            if [ -d "\$dir" ]; then
              exec $out/bin/.librewolf --profile "\$dir" "\$@"
            else
              mkdir -p -m 700 "\$dir"
              trap '${save} "\$dir/cookies.sqlite" "\$state/cookies.sqlite"; rm -rf "\$dir"' EXIT

              cp -rL --no-preserve=mode ${profile}/. "\$dir"
              ${load} "\$dir/cookies.sqlite" "\$state/cookies.sqlite"

              $out/bin/.librewolf --profile "\$dir" "\$@"
            fi

            EOF

            chmod +x $out/bin/librewolf
          '';

          meta = {
            mainProgram = "librewolf";
          };
        };

1 Like