Skip to content

feat(nix): contribute nix devcontainer feature #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/nix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Nix

Installs the Nix package manager.

## Example Usage

```json
"features": {
"ghcr.io/devcontainers/features/nix:1": {
"version": "latest"
}
}
```

## Options

| Options Id | Description | Type | Default Value |
|-----|-----|-----|-----|
| version | Select or enter a Nix version to install | string | latest |

---

_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/devcontainers/features/blob/main/src/node/devcontainer-feature.json). Add additional notes to a `NOTES.md`._
26 changes: 26 additions & 0 deletions src/nix/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "nix",
"version": "1.0.0",
"name": "Nix",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/nix",
"description": "Installs Nix which is a tool that takes a unique approach to package management and system configuration",
"options": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have an option for a set of Nix packages to include in the installation? That way the package downloads can be done during an image prebuild to save time later on when users create their dev environment from the image.

"version": {
"type": "string",
"enum": [
"latest"
],
"default": "latest",
"description": "Currently unused."
}
},
"customizations": {
"vscode": {
"extensions": [
"bbenoist.Nix"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the most popular Nix extension on the VS Code marketplace, it looks like it hasn't been updated since 2015 and the maintainer hasn't responded to several issues recently: https://github.com/bbenoist/vscode-nix. I don't think we should recommend an extension that isn't actively maintained.

]
}
},
"containerEnv": {
}
}
24 changes: 24 additions & 0 deletions src/nix/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
#
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/sshd.md
# Maintainer: The VS Code and Codespaces Teams
#

set -e

if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi


sh <(curl -L https://nixos.org/nix/install) --daemon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with Nix. Do you know if it works well in daemon mode in docker containers? Do we need to add anything to the entrypoint to start the daemon on container startup?

Copy link
Member

@Chuxel Chuxel Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah an enterpoint would be needed. This I think would also require the container run as root - which isn't a problem for services like Codespaces, GitPod, or when using Docker Desktop, but is a consideration otherwise.

Most of the exploration I did for https://github.com/Chuxel/feature-library/blob/main/src/nix/install.sh was trying to figure out a way to use single user unless the UID/GID changed. I think I have a working model - it sets things up so you can run in either daemon or non-daemon mode (which the nix install script itself couldn't do, but the steps it takes are documented for both scenarios). We could opt to start from here as a baseline if preferrable.

It also verifies the download using their GPG key, has support for referencing a list of packages, a flake, or a derivation.

It also should theoretically work on debian, redhat, and alpine based distros - the main difference is native packages that should be installed and I tweaked the utility functions to do that to detect and install the right one. Something we could do in other cases as well.


mkdir -p $HOME/.config/nix $HOME/.config/nixpkgs
echo 'sandbox = false' >> $HOME/.config/nix/nix.conf
echo '{ allowUnfree = true; }' >> $HOME/.config/nixpkgs/config.nix
echo '. $HOME/.nix-profile/etc/profile.d/nix.sh' >> $HOME/.bashrc
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user uses any other shell than bash then this would break. Not sure how to resolve - opening PR up for access to review hivemind knowledge.

Comment on lines +21 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect that we'll want these setup in the $HOME of the remoteUser. This is a bit tricky today, and devcontainers/spec#91 is tracking passing in the remoteUser as an environment variable to feature install scripts. It might be simplest to wait for that change to land to keep this feature script simple.

12 changes: 12 additions & 0 deletions test/nix/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" nix --version

# Report result
reportResults