Skip to content

Building Packages Using Debx Command Line

Mosquito edited this page May 4, 2025 · 2 revisions

Debx provides a convenient command-line interface for creating, unpacking, and inspecting Debian packages. This page explains how to use the debx command-line tool to build packages without writing Python code.

Installation

First, install the Debx package using pip:

pip install debx

Basic Package Creation

The debx pack command allows you to create a Debian package by specifying control files and data files directly from the command line.

Command Syntax

debx pack \
    --control source_path:destination_path[:modifiers] [...] \
    --data source_path:destination_path[:modifiers] [...] \
    --output output.deb

The format for specifying files is:

source_path:destination_path[:modifiers]

Where:

  • source_path is the path to a file or directory on your system
  • destination_path is the absolute path where the file should be placed inside the package
  • modifiers (optional) are additional parameters to modify file attributes

Available Modifiers

  • mode=0755 - Set file permissions (in octal)
  • uid=1000 - Set file owner ID
  • gid=1000 - Set file group ID
  • mtime=1234567890 - Set file modification time (Unix timestamp)

Creating a Simple Package

Here's a simple example of creating a basic Debian package:

# First, create necessary files
echo "Package: hello-world
Version: 1.0.0
Architecture: all
Maintainer: Your Name <your.email@example.com>
Description: Hello World package
 This is a simple hello world package created with debx." > control

echo '#!/bin/sh
echo "Hello, World!"' > hello-world
chmod +x hello-world

# Create the package
debx pack \
    --control control:control \
    --data hello-world:/usr/bin/hello-world:mode=0755 \
    --output hello-world_1.0.0_all.deb

Creating a More Complex Package

For a more complex package with multiple files and directories:

# Create a directory structure
mkdir -p myapp/{bin,etc,share/doc}

# Create executable
cat > myapp/bin/myapp << 'EOF'
#!/bin/sh
echo "Running My Application"
if [ -f /etc/myapp/config ]; then
    echo "Using configuration from /etc/myapp/config"
    cat /etc/myapp/config
fi
EOF
chmod +x myapp/bin/myapp

# Create configuration file
cat > myapp/etc/config << 'EOF'
# Configuration for myapp
LOG_LEVEL=info
PORT=8080
EOF

# Create documentation
cat > myapp/share/doc/README.md << 'EOF'
# My Application

This is a sample application created with debx.

## Usage

Simply run `myapp` from the command line.
EOF

# Create control file
cat > control << 'EOF'
Package: myapp
Version: 1.0.0
Architecture: all
Maintainer: Your Name <your.email@example.com>
Description: My Sample Application
 A demonstration of creating a more complex package
 with debx command line interface.
EOF

# Create maintainer scripts
cat > postinst << 'EOF'
#!/bin/sh
echo "Configuring myapp..."
exit 0
EOF
chmod +x postinst

# Create the package with multiple files specified together
debx pack \
    --control control:control \
              postinst:postinst:mode=0755 \
    --data myapp/bin/myapp:/usr/bin/myapp:mode=0755 \
           myapp/etc/config:/etc/myapp/config \
           myapp/share/doc:/usr/share/doc/myapp \
    --output myapp_1.0.0_all.deb

Adding Entire Directories

When specifying a directory as the source path, all files within that directory will be included in the package while preserving the directory structure:

# Create a directory with multiple files
mkdir -p app_files/{bin,lib,doc}
echo '#!/bin/sh
echo "Main application"' > app_files/bin/app
chmod +x app_files/bin/app

echo '#!/bin/sh
echo "Helper tool"' > app_files/bin/helper
chmod +x app_files/bin/helper

echo "Sample library" > app_files/lib/library.dat
echo "Documentation" > app_files/doc/README.md

# Create control file
echo "Package: app
Version: 1.0.0
Architecture: all
Maintainer: Your Name <your.email@example.com>
Description: Sample application
 An application with multiple files." > control

# Create the package, including the entire app_files directory
debx pack \
    --control control:control \
    --data app_files:/usr/share/app \
    --output app_1.0.0_all.deb

This will include all files from the app_files directory, preserving the directory structure under /usr/share/app in the package.

Controlling File Permissions

When adding files to a package, you can use the mode modifier to set specific permissions. You can specify multiple files in a single --data argument:

debx pack \
    --control control:control \
    --data script.sh:/usr/bin/script:mode=0755 \
           config.conf:/etc/app/config.conf:mode=0644 \
    --output app.deb

Setting Owner and Group

You can set the owner and group for files in the package, specifying multiple files in a single command:

debx pack \
    --control control:control \
    --data app:/usr/bin/app:mode=0755,uid=0,gid=0 \
           data:/var/lib/app/data:uid=1000,gid=1000 \
    --output app.deb

Advanced Package Creation

Here's a more comprehensive example that includes multiple control files and data files, all specified with fewer command-line arguments:

# Create control file
cat > control << 'EOF'
Package: myapp
Version: 1.2.3
Architecture: amd64
Maintainer: Your Name <your.email@example.com>
Depends: libc6 (>= 2.14), libsomething (>= 1.0)
Description: My Application Package
 This package contains a wonderful application
 with multiple features.
 .
 Second paragraph of the description.
EOF

# Create preinst script
cat > preinst << 'EOF'
#!/bin/sh
echo "Preparing to install..."
exit 0
EOF
chmod +x preinst

# Create postinst script
cat > postinst << 'EOF'
#!/bin/sh
echo "Configuring package..."
# Create required directories
mkdir -p /var/lib/myapp
# Set permissions
chown -R myapp:myapp /var/lib/myapp
exit 0
EOF
chmod +x postinst

# Create application binary
cat > myapp << 'EOF'
#!/bin/sh
echo "Running MyApp version 1.2.3"
exit 0
EOF
chmod +x myapp

# Create configuration file
cat > config.conf << 'EOF'
# Configuration for myapp
port = 8080
log_level = info
EOF

# Create the package with multiple files specified in each argument
debx pack \
    --control control:control \
              preinst:preinst:mode=0755 \
              postinst:postinst:mode=0755 \
    --data myapp:/usr/bin/myapp:mode=0755 \
           config.conf:/etc/myapp/config.conf \
    --output myapp_1.2.3_amd64.deb

Inspecting Packages

You can use the debx inspect command to examine the contents of a package:

# Inspect a package using ls-like format (default)
debx inspect myapp_1.0.0_all.deb

# Show just the file names
debx inspect --format=find myapp_1.0.0_all.deb

# Output in JSON format
debx inspect --format=json myapp_1.0.0_all.deb

# Output in CSV format
debx inspect --format=csv myapp_1.0.0_all.deb

Unpacking Packages

The debx unpack command extracts a .deb package into a directory:

# Unpack a package
debx unpack myapp_1.0.0_all.deb

# Unpack to a specific directory
debx unpack myapp_1.0.0_all.deb --directory /tmp/myapp

This will extract the internal AR archive members and tar archives (debian-binary, control.tar.gz, data.tar.bz2) into the specified directory.

Best Practices

When building Debian packages with the debx command line:

  1. Follow the Debian packaging conventions for file placement:

    • Executables should go in /usr/bin/
    • Configuration files should go in /etc/
    • Data files should go in /usr/share/package-name/
    • Documentation should go in /usr/share/doc/package-name/
  2. Ensure all executable files have the appropriate permissions (mode=0755)

  3. Include comprehensive metadata in the control file

  4. Use absolute paths (starting with "/") for all destination paths

  5. Include maintainer scripts when necessary (preinst, postinst, prerm, postrm)

  6. For larger projects, consider creating a build script that generates the necessary files and calls debx

Conclusion

The debx command line interface provides a simple yet powerful way to create Debian packages without requiring external tools or writing Python code. It's especially useful for quick packaging tasks and automation scripts.

For more complex packaging needs, you might want to use the Debx Python library directly.