Skip to main content

Your submission was sent successfully! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates from Canonical and upcoming events where you can meet our team.Close

Thank you for contacting us. A member of our team will be in touch shortly. Close

  1. Blog
  2. Article

Jeremie Deray
on 3 July 2020


In this post, we will see how we can containerize our home in LXD simply managing our personal configuration files – a.k.a. dotfiles. Yeah dotfiles, named after their common ~/.my_config form, you know, all of those small configuration files lying across our $HOME. In other words, how one can change the house while keeping the furniture and decoration in place.

Because there is no place like $HOME

Since we are spending so much time on our machine, be it for work or for fun (maybe both?), we love to tweak our environment to our taste and needs. Change the UX, create some aliases, using a dark theme and what not. Most, if not all of these are saved in some configuration files somewhere. And since we spent so much time making a home for ourselves, wouldn’t it be great if we could quickly set it up again on a different computer? This is precisely what we are going to see here.

In a previous post, we saw how to set up a LXD container for developing with ROS (now also available with an accompanying video). Here we will see how we can quickly set up our personal development environment in the said containers and enable a tidy and seamless workflow.

Picking a dotfiles manager

A dotfiles manager is essentially a piece of software that takes care of your configuration files for you. What does that mean? Well I would personally consider the bare minimum features to include versioning, preferably through git, and of course the installation of the files to their correct location, as they are typically expected to be found at a given path. But we may have different expectations from a dotfiles manager based on our needs and habits. Looking on the web for such manager, you may encounter many of them – find a whole list of them here. Most of them work off the same principles, being a small set of utils to help manage our dotfiles.

In this post we settled using chezmoi. It is vastly popular, open source and is available as a snap making it usable virtually anywhere. Some other nice features include being git-based, being cli-based, and supporting multi dotfiles repos. It is chezmoi which will set up our home in LXD as we will see.

You may want to give a look at the aforementioned list of managers and pick one that best answers your needs and expectations. Note that many of them are interchangeable, so you can get started with chezmoi for now and later move to another one as you see fit.

Alright so how do we get started?

Creating a dotfiles repository

Before creating our dotfiles backup, we need to install the manager. To install chezmoi, nothing easier, simply use the command:

$ snap install chezmoi --classic

And we are done. 

We can now create our git-based dotfiles repository and start filling it up. To create the repository managed by chezmoi, simply enter:

$ chezmoi init

This command creates an empty repository in ~/.local/share/chezmoi.

To backup a dotfile and populate our repository, we make use of the add command:

$ chezmoi add .bashrc

The command copies the file in the repository managed by chezmoi at ~/.local/share/chezmoi/dot_bashrc.

Now all we have to do is to commit our change and save our repository online. The command,

$ chezmoi cd

places us directly in the git repository where we can use the usual git commands,

$ git add dot_bashrc
$ git commit -m 'add .bashrc'

Finally we can save our dotfiles online, e.g. on GitHub,

$ git remote add origin [email protected]:user/dotfiles.git
$ git push -u origin master

We should now repeat this operation for each and every configuration file we would like to save. Note that if you have secrets in your dotfiles, say some credentials, SSH keys and such, chezmoi offer many options for your secrets to remain as such.

With our dotfiles safely backed up online, we will now see how we can quickly set up our environment on a new machine.

Quickly setting up a new machine

Whether you bought a new computer or nuked your old hardware with a fresh new distro, you will now witness the true power of chezmoi.

To install our cosy environment on a fresh distro, all we have to do is,

1. Install chezmoi 

$ snap install chezmoi --classic

2. Import our dotfiles

$ chezmoi init https://github.com/username/dotfiles.git

3. Let chezmoi works its magic,

$ chezmoi apply

Voila! Home sweet home.

Of course this post is only a quick overview of a given dotfiles manager. I won’t detail here all of its options and features and let you discover them for yourself in its documentation page.

At this point you may be wondering if this is really worth it. You probably install a fresh distro every 2 years or so and completely change hardware even less frequently. So why bother? Well, fellow developer, aren’t you using containers? If not, you definitely should consider it and check the aforementioned post where we detail a development workflow for ROS in LXD.

A disposable tiny home in LXD

If you are like me, trying your best to keep a tidy laptop while messing around with plenty of different software toys, then you may have had one of these days during which you spawn several containers. Containers in which we don’t have our sweet bash aliases; on our very own machine! But thanks to chezmoi we can now start up a fresh container and have it mimic $HOME in a matter of seconds. Let me demonstrate it for you with a LXD container,

$ lxc launch ubuntu:20.04 tmp-20-04
$ lxc profile add chezmoi tmp-20-04
$ lxc ubuntu tmp-20-04

Ahhh, what a cozy tiny disposable home in LXD!

That seemed too easy to you? Alright I confess, I used some of my own LXD profile and aliases here. But isn’t it what this whole post is about? In case you want to use the same kind of shortcuts, allow me to point you once more to our LXD blog post. Anyway, note that the above 3 lines really boils down to,

$ lxc launch ubuntu:20.04 tmp-20-04
$ lxc exec tmp-20-04 -- sudo --login --user ubuntu
...
$ snap install chezmoi --classic
$ chezmoi init https://github.com/username/dotfiles.git
$ chezmoi apply

With this example, I hope that I managed to offer you a glimpse at the power of chezmoi (and more generally of dotfiles managers), especially when coupled to a containerized workflow.

Before closing, let me give you one last tip. Because we made our containerized workflow rather seamless, it can be easy to lose track of which shell is in a container and which is not. To differentiate them, add the following to your .bashrc:

function prompt_lxc_header()
{
  if [ -e /dev/lxd/sock ]; then
    echo "[LXC] ";
  fi
}
PS1='$(prompt_lxc_header)'$PS1

When used in a container, a shell prompt in the said container will now look something like:

[LXC] ubuntu@tmp-20-04:~$

No more confusion.

Photo by Albert13377 on Wikimedia Commons.

Related posts


Gabriel Aguiar Noury
9 October 2024

Deploying scalable AI and real-time robots at ROSCon 24

Robotics Article

Another year, another ROSCon! This year we’re setting off to Odense, Denmark. At Canonical, we are excited to once again sponsor this event for a community that means so much to us. Just like last year, we are eager for the talks and workshops that, as always, teach us new things and inspire us to ...


gbeuzeboc
25 September 2024

TurtleBot3 OpenCR firmware update from a snap

IoT Article

The TurtleBot3 robot is a standard platform robot in the ROS community, and it’s a reference that Canonical knows well, since we’ve used it in our tutorials. As a matter of fact, we use it to demonstrate some of our work, such as distributing a ROS stack through snaps. This robot embeds two boards, a ...


Gabriel Aguiar Noury
17 October 2023

Discover the future of Autonomous Mobile Robots (AMR) with Canonical and Husarion at ROSCon 2023

Robotics Article

At a time when Autonomous Mobile Robots (AMR) keep rapidly reshaping industries like logistics and manufacturing, we’re thrilled to introduce you to our ROSCon 2023 Platform Partner; Husarion. Together, we are merging the world-class capabilities of Ubuntu Core with Husarion’s state-of-the-art robotics platforms. As we unveil our collabor ...