`discourse` CLI for NixOS packaged Discourse

Hi folks,

With joy I found that Discourse is packaged and I can easily configure and deploy it and it works great. Thank you mainainers!

The only thing I miss is discourse CLI command that is by default present in containerized version of Discourse.

I looked into the container how it works I realized that discourse is just a very simple shell script that executes bundle. I looked at the NixOS discourse.service and I executed the same commands like I was in a container using discourse shell script:

  • first I login as discourse
su discourse -s `which bash`
  • by trial / error I realized I need some env var (as they’re in the discourse.service):
export HOME=/run/discourse/home
export MALLOC_ARENA_MAX=2
export RAILS_ENV=production
export UNICORN_LISTENER=/run/discourse/sockets/unicorn.sock
export UNICORN_SIDEKIQS=1
export UNICORN_TIMEOUT=30
  • and then execute
cd /nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse

/nix/store/23m9lin6g1gjj7v7p4jvfgyw2xd7sqsa-discourse-ruby-env-3.2.2/bin/bundle exec script/discourse enable_restore

fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
Restore are now permitted. Disable them with `disable_restore`
  • and that worked, now restore:
/nix/store/23m9lin6g1gjj7v7p4jvfgyw2xd7sqsa-discourse-ruby-env-3.2.2/bin/bundle exec script/discourse restore /tmp/my-forum-2024-06-01-031039-v20240425133407.tar.gz

discourse restore /tmp/my-forum-2024-06-01-031039-v20240425133407.tar.gz
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
Starting restore: /tmp/my-forum-2024-06-01-031039-v20240425133407.tar.gz
[STARTED]
'system' has started the restore!
Marking restore as running...
Making sure /nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/tmp/restores/default/2024-07-05-041305 exists...
Copying archive to tmp directory...
EXCEPTION: /nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/discourse.rb:138:in `exec': Failed to copy archive to tmp directory.
cp: cannot stat '/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/public/backups/default/tmp/my-forum-2024-06-01-031039-v20240425133407.tar.gz': No such file or directory
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/discourse.rb:172:in `execute_command'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/discourse.rb:138:in `exec'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/discourse.rb:34:in `execute_command'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/backup_restore/local_backup_store.rb:46:in `download_file'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/backup_restore/backup_file_handler.rb:62:in `copy_archive_to_tmp_directory'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/backup_restore/backup_file_handler.rb:22:in `decompress'
/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/lib/backup_restore/restorer.rb:42:in `run'
script/discourse:149:in `restore'
/nix/store/9wsxc0cirvk6fippc0zc8z8yswlyklk0-ruby3.2-thor-1.3.0/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
/nix/store/9wsxc0cirvk6fippc0zc8z8yswlyklk0-ruby3.2-thor-1.3.0/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
/nix/store/9wsxc0cirvk6fippc0zc8z8yswlyklk0-ruby3.2-thor-1.3.0/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
/nix/store/9wsxc0cirvk6fippc0zc8z8yswlyklk0-ruby3.2-thor-1.3.0/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/base.rb:584:in `start'
script/discourse:290:in `<main>'
Trying to rollback...
There was no need to rollback
Cleaning stuff up...
Removing tmp '/nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/tmp/restores/default/2024-07-05-041305' directory...
Marking restore as finished...
Notifying 'system' of the end of the restore...
Finished!
[FAILED]
Restore done.
  • that failed, by assumption is that it wanted to write into read-only Nix store, but that’s not a problem since:
ls -l /nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/public/backups

lrwxrwxrwx 1 discourse discourse 26 Jul  5 03:57 /nix/store/r8qpj1bjvjqk56c7nsdlf3j695khnfsg-discourse-3.2.2/share/discourse/public/backups -> /var/lib/discourse/backups

And I checked that /var/lib/discourse/backups writable for discourse user.

Any ideas?

Thank you.

I’ve never used the restore feature on NIxOS (my Discourse is still their official Docker) but I used restore in the Docker version and it works OK. I looked at env in my Docker Discourse and you might need to add some of them:

LEFTHOOK=0
LETSENCRYPT_ACCOUNT_EMAIL=xxxxxxxxxxxxxxxxxxxxxx
HOSTNAME=xxxxxxxxxxxxxxxxxxxxxx
LANGUAGE=en_US.UTF-8
UNICORN_WORKERS=4
DISCOURSE_HOSTNAME=xxxxxxxxxxxxxxxxxxxxxx
RUBY_GC_HEAP_INIT_SLOTS=400000
DISCOURSE_SMTP_USER_NAME=xxxxxxxxxxxxxxxxxxxxxx
DISCOURSE_SMTP_ADDRESS=xxxxxxxxxxxxxxxxxxxxxx
DOCKER_HOST_IP=
RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=1.5
RUBY_VERSION=3.3.2
PWD=/var/www/discourse
DISCOURSE_DB_SOCKET=/var/run/postgresql
DISCOURSE_DEVELOPER_EMAILS=xxxxxxxxxxxxxxxxxxxxxx
HOME=/root
LANG=en_US.UTF-8
CARGO_HOME=/usr/local/cargo
DISCOURSE_SMTP_PORT=587
RUBY_GC_HEAP_GROWTH_MAX_SLOTS=40000
DEBIAN_RELEASE=bullseye
DISCOURSE_SMTP_PASSWORD=xxxxxxxxxxxxxxxxxxxxxx
DISCOURSE_NOTIFICATION_EMAIL=xxxxxxxxxxxxxxxxxxxxxx
PG_MAJOR=13
DISCOURSE_DB_HOST=
TERM=xterm
RUSTUP_HOME=/usr/local/rustup
DISCOURSE_FORCE_HTTPS=true
RUBY_ALLOCATOR=/usr/lib/libjemalloc.so
SHLVL=1
DISCOURSE_DB_PORT=
LETSENCRYPT_DIR=/shared/letsencrypt
DISCOURSE_SMTP_DOMAIN=xxxxxxxxxxxxxxxxxxxxxx
UNICORN_SIDEKIQS=1
LC_ALL=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RAILS_ENV=production
OLDPWD=/
_=/usr/bin/env