pytest-postgresql in nix in docker

2025-01-29

We use GitLab CI and Nix for our projects. For Python projects too. And we use pytest for Python projects. And when we use PostgreSQL, we use pytest-postgresql. Therefore, somewhere in our .gitlab-ci.yml, there is something like:

test:
  image: nixos/nix
  script:
    - nix develop -c pytest

If you don’t see imediately why tests must fail, I offer the single of about 22 thousands of logged lines:

pg_ctl: cannot be run as root

I’ve spent two days trying to find out how to persuade nixos/nix to let me run pytest under non-root. I offer you the following:

test:
  image: nixos/nix
  script: |
    nix profile install nixpkgs#su
    mkdir /etc/pam.d
    echo 'auth sufficient pam_rootok.so' > /etc/pam.d/su
    echo 'account required pam_permit.so' >> /etc/pam.d/su
    echo 'session required pam_permit.so' >> /etc/pam.d/su
    echo "user::1000:1000::/var/empty:$(cat /etc/passwd | grep root | cut -d':' -f7)" >> /etc/passwd
    nix develop -c su -c pytest user

Please, note that we don’t need to run nix develop as non-root, which is completely different problem.

go back