- Introduction
- Features
- Prerequisites
- Installation
- Usage
- Build and Deployment
- Contributing
- License
- Contact
Welcome to the Alpine + Nix → NixOS project! This repository provides a streamlined environment that combines the lightweight and security-focused nature of Alpine Linux with the powerful and reproducible package management capabilities of Nix. The result is a flexible and efficient system reminiscent of NixOS, tailored for both development and production environments.
- Lightweight Base: Utilizes Alpine Linux, known for its minimal footprint and security-oriented design.
- Reproducible Package Management: Leverages Nix for reliable and reproducible builds, atomic upgrades, and rollbacks.
- Multi-Architecture Support: Built to support multiple architectures, including
amd64
andarm64
. - Automated CI/CD: Integrated GitHub Actions for continuous integration and deployment to GitHub Container Registry (GHCR) and Docker Hub.
- Secure Environment: All binaries are compiled with security features such as Position Independent Executables (PIE) and stack smashing protection.
Before you begin, ensure you have met the following requirements:
- Docker: Install Docker to build and run the containerized environment.
- GitHub Account: Required for accessing the repository and configuring GitHub Actions.
- Docker Hub Account: Optional, for deploying images to Docker Hub.
-
Clone the Repository
git clone https://github.com/redoracle/nixos.git cd nixos
-
Build the Docker Image
docker build -t ghcr.io/redoracle/nixos:latest .
-
Run the Docker Container
docker run -it ghcr.io/redoracle/nixos:latest /bin/bash
If you prefer to install Nix manually on Alpine Linux:
-
Ensure Dependencies are Installed
apk update && apk upgrade apk add --no-cache bash curl sudo openssl grep sed xz gnupg
-
Download and Run the Installation Script
curl -O https://raw.githubusercontent.com/redoracle/nixos/refs/heads/master/install_nix.sh chmod +x install_nix.sh ./install_nix.sh
Once installed, you can use Nix to manage packages within your Alpine Linux environment. Some common Nix commands include:
-
Install a Package
nix-env -iA nixpkgs.package-name
-
Upgrade Packages
nix-env -u
-
Rollback to Previous Configuration
nix-env --rollback
For more detailed usage, refer to the Nix Manual.
This project utilizes GitHub Actions to automate the build and deployment process. The workflow is triggered on pushes to the main
or master
branches, on manual dispatch, and is scheduled to run every Sunday.
The workflow performs the following steps:
- Checkout Repository: Retrieves the latest code from the repository.
- Set Up Docker Buildx: Prepares the Docker builder for multi-platform builds.
- Authenticate with Registries: Logs into GitHub Container Registry (GHCR) and Docker Hub.
- Get Nix Version: Extracts the installed Nix version for tagging the image and releases.
- Build and Push Docker Image: Builds the Docker image and pushes it to both GHCR and Docker Hub.
- Create Release: Automatically creates a new GitHub release if the version does not exist.
- Cleanup: Prunes unused Docker builder cache to optimize runner resources.
The GitHub Actions workflow is defined in .github/workflows/docker-deploy.yml
. Ensure you have set the following secrets in your GitHub repository:
DOCKER_USERNAME
: Your Docker Hub username.DOCKER_PASSWORD
: Your Docker Hub password or access token.
name: Build and Deploy NixOS Docker Image
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0' # Every Sunday at midnight UTC
push:
branches:
- master
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Checkout the repository
- name: Checkout repository
uses: actions/checkout@v4
# Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Log in to GitHub Container Registry (GHCR)
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Get Nix version from the Dockerfile
- name: Get Nix version
id: get_version
run: |
NIX_VERSION=$(docker run --rm $(docker build -q .) nix-env --version | cut -d ' ' -f 3)
echo "NIX_VERSION=$NIX_VERSION" >> $GITHUB_ENV
# Build and Push Docker Image
- name: Build and Push Docker Image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: |
ghcr.io/${{ github.repository_owner }}/nixos:${{ env.NIX_VERSION }}
ghcr.io/${{ github.repository_owner }}/nixos:latest
${{ secrets.DOCKER_USERNAME }}/nixos:${{ env.NIX_VERSION }}
${{ secrets.DOCKER_USERNAME }}/nixos:latest
# Create GitHub release if it does not exist
- name: Create GitHub release
if: env.create_release == 'true'
uses: actions/create-release@v1
with:
tag_name: v${{ env.NIX_VERSION }}
release_name: NixOS Docker Image v${{ env.NIX_VERSION }}
draft: false
prerelease: false
body: |
NixOS Docker image version ${{ env.NIX_VERSION }} has been built and released.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Post-build cleanup (optional)
- name: Docker prune
run: docker builder prune --force
Contributions are welcome! To contribute:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add some feature"
-
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
Please ensure your contributions adhere to the following guidelines:
- Follow the existing coding style.
- Write clear and concise commit messages.
- Include relevant documentation and tests.
This project is licensed under the MIT License.
For any questions or support, please open an issue
### What has been added:
1. **Nix
Version Retrieval:**
- Added a new workflow step that fetches the Nix version from the Docker build and uses it for tagging the Docker image and creating GitHub releases.
2. **Image Publishing:**
- Updated the Docker build and push step to push images to both Docker Hub and GHCR with versioned and `latest` tags.
3. **GitHub Release Creation:**
- Added a step to create a new GitHub release if a release with the Nix version tag doesn’t already exist.
4. **Scheduled Build:**
- The workflow now runs automatically every Sunday at midnight UTC.