Is there a guide to using GitHub to store and restore your configs (from a beginner perspective)? I’ve been copying configs by USB to date but it is time to start a better way. I’ve created an account, but otherwise never used GitHub. All my searches get lots of returns, but none actually what I am looking for. Thx
GitHub is both a code storage and sharing platform, so people use it to store their own code as well as to browse and reference code created by others. When you use a Nix “channel” (or many flake inputs), you’re actually pointing at a collection of .nix files downloaded from a GitHub repository.
GitHub is built around a tool called Git, which is a tool that helps in both version-tracking code and collaborating with others by merging multiple users’ changes to the same code. Here is a pretty good introduction to Git from MIT’s Missing Semester course. I would also check out GitHub’s official tutorials. Git is a complex program, but you can get away with knowing just the basics.
Git is the tool, whereas GitHub a remote server that you can push to using Git. There are many ways to authenticate to GitHub, whether using an SSH key, account token, or using one of their official programs. Consider whether or not your text editor has any built-in GitHub integrations.
The benefits of using Git and a remote (such as GitHub) can include the following:
- Maintain a history of your code, allowing you to go back to previous changes if necessary.
- Store your code in a third-party location, safe from accidental deletion.
- Allows you to “clone” your code to a new device when you’re setting it up.
- Allows you to share your code with others, especially if you’re asking for help on a forum such as this.
- Allows others to submit suggestions to your code, which you can merge in with yours if you so choose.
- Allows you to “fork” others’ code and make your own changes (even to nixpkgs!), which you can then submit back upstream.
- With Nix, you can even reference GitHub or other remote repositories when packaging code or while using flakes without even bothering to download it first. That means I could start up a new blank system with Nix, and then switch to my configuration with a single command (e.g.
nixos-rebuild --switch --flake github:my-account/my-repository#my-system).
It’s hard to offer a guide to learning Git, GitHub, and Nix/NixOS together, just because the learning curve for each of them is fairly steep on their own. Nix is fairly advanced and the community expects most to already be aware of these tools. That’s why I suggest finding guides for each tool separately.
Remember that you can use git on its own for many different things without having to worry about 3rd party hosting services such as github/sourcehut/gitlab/gitea.
This is all you need in an empty directory to get started.
Thanks @noah - an hour later and I have achieved most of what I wanted to achieve. The only missing piece so far is how do I take the files in the repository and download them into /etc/nixos. I then have a backup/restore for all my machines with version control and “synced” common config aspects
So you have a couple of options for what you can do here.:
- Remove your current
/etc/nixosdirectory and move your Git directory into
/etc/to act as a
- You could also symlink to it if you wanted to keep editing the files from your home directory.
- Or keep your Git directory where it is, and when you rebuild just refer to it as your config:
nixos-rebuild switch -I nixos-config=/path/to/git/dir/configuration.nix. See this wiki page for more info.
- Or if you choose to use flakes, you can also point to a Git repo anywhere on your system:
nixos-rebuild switch --flake /path/to/git/dir
Check out this thread for more discussion of this issue.