You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I explained in #170 (comment), starting services when starting a nix-shell is kind of messy. Mostly for background services, such as Postgres. That was a conceptual failure for development IMHO.
This is how I build my shell (of course, pkgs has the usual devshell overlay, etc.):
pkgs.devshell.mkShell{imports=[# Includes postgres command, env and initial db setup"${pkgs.devshell.extraModulesDir}/services/postgres.nix"];# Packages not displayed on menudevshell.packages=withpkgs;[# Reaps services when exiting parent process (like your IDE)tini# Service helpers(writeScriptBin"service-postgres"'' #!/usr/bin/env bash exec tini -sp SIGTERM -- postgres '')];}
The writeScriptBin part could have been replaced by devshell-files alias, but I felt like it was adding too much dependency for such a simple task. However, if you're using it, it's more ergonomic.
An important part however is tini. It makes sure that, when exiting the IDE, all postgres subprocesses are properly reaped. Close the IDE, all is gone! Cool 😎
Now I add a task for the IDE (VSCode in my case):
// .vscode/tasks.json{"version": "2.0.0","tasks": [{"label": "Postgres",// This is the important part: how it relates to nix-shell"type": "shell","command": "service-postgres","options": {"shell": {"executable": "nix-shell","args": ["--run"]}},// The rest is just required boilerplate to make it work fine"isBackground": true,"problemMatcher": [{"pattern": [{"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$","file": 1,"location": 2,"message": 3}],"background": {"activeOnStart": true,"beginsPattern": "starting PostgreSQL","endsPattern": "database system is ready to accept connections"}}],"presentation": {"reveal": "silent"}},]}
Now I want Postgres to autostart when I start debugging a Python file, so I create this launch configuration:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
As I explained in #170 (comment), starting services when starting a
nix-shell
is kind of messy. Mostly for background services, such as Postgres. That was a conceptual failure for development IMHO.I tried with devshell-files and its s6-based services backend, but also didn't work out fine. It had a different purpose of what I wanted.
Well, here's how I did it.
This is how I build my shell (of course, pkgs has the usual devshell overlay, etc.):
The
writeScriptBin
part could have been replaced by devshell-files alias, but I felt like it was adding too much dependency for such a simple task. However, if you're using it, it's more ergonomic.An important part however is tini. It makes sure that, when exiting the IDE, all postgres subprocesses are properly reaped. Close the IDE, all is gone! Cool 😎
Now I add a task for the IDE (VSCode in my case):
Now I want Postgres to autostart when I start debugging a Python file, so I create this launch configuration:
Now all I have to do is to press F5 and:
nix-shell
gets all required dependencies.devshell/extra/services/postgres.nix
Lines 49 to 58 in d97df53
I'm quite happy with it! ❤️
Beta Was this translation helpful? Give feedback.
All reactions