nix-community / nixbox

NixOS Vagrant boxes [maintainer=@ifurther]
https://app.vagrantup.com/nixbox/
MIT License
309 stars 101 forks source link

Example Vagrantfile for a repository containing shell.nix #19

Closed knedlsepp closed 7 years ago

knedlsepp commented 7 years ago

I'm in the situation that I have defined a "shell.nix" file inside one of my projects. I would like to be able to set up a Vagrantfile, which uses your nixbox and automatically drops you in a nix-shell for the specified file on vagrant up; vagrant ssh. Is this use case possible using your nixbox?

zimbatm commented 7 years ago

You could replace /home/vagrant/.bashrc with something like:

if [[ -z $PS1 ]]; then return; fi # for scp and friends
if [[ -z $IN_NIX_SHELL ]]; then exec nix-shell /vagrant; fi

# the usual stuff here

When the user runs vagrant ssh it will log him into bash and automatically start nix-shell, which loads bash again but this time skips the second branch.

knedlsepp commented 7 years ago

Thank you for the great solution! Here is the finished Vagrantfile. I did use .profile, because .bashrc was not being used.

Vagrant.configure("2") do |config|
  config.vm.box = "nixos/nixos-16.09-x86_64"
  config.vm.provider "virtualbox" do |v|
    v.memory = 4096
    v.cpus = 2
  end

  config.vm.provision "shell", inline: <<-SHELL
    mkdir ~vagrant/.nixpkgs
    echo '{ allowUnfree = true; }' > ~vagrant/.nixpkgs/config.nix
    echo 'if [[ -z $PS1 ]]; then return; fi # for scp and friends' >> ~vagrant/.profile
    echo 'if [[ -z $IN_NIX_SHELL ]]; then exec nix-shell /vagrant/shell.nix; fi' >> ~vagrant/.profile
    echo 'Done provisioning.'
  SHELL
end