aboutsummaryrefslogtreecommitdiffstats
path: root/garden/arch-pkg-guide.md
blob: b38807855b9639842be41c5616ad9e97c290f5f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
title: Arch Linux Packaging Guide

*Generated using [Claude.ai](https://claude.ai) (Opus 4.5 model)*

# Complete Guide to Creating Arch Linux Packages

A comprehensive guide consolidating official Arch Linux documentation on creating, building, and submitting packages to the Arch User Repository (AUR).

---

## Table of Contents

1. [Overview](#1-overview)
2. [Prerequisites and Setup](#2-prerequisites-and-setup)
3. [Understanding the PKGBUILD File](#3-understanding-the-pkgbuild-file)
4. [PKGBUILD Variables Reference](#4-pkgbuild-variables-reference)
5. [PKGBUILD Functions](#5-pkgbuild-functions)
6. [Building Packages with makepkg](#6-building-packages-with-makepkg)
7. [VCS Package Guidelines](#7-vcs-package-guidelines)
8. [Testing and Quality Assurance](#8-testing-and-quality-assurance)
9. [Submitting to the AUR](#9-submitting-to-the-aur)
10. [Package Maintenance](#10-package-maintenance)
11. [Best Practices and Common Mistakes](#11-best-practices-and-common-mistakes)
12. [Quick Reference](#12-quick-reference)

---

## 1. Overview

### What is an Arch Linux Package?

An Arch package is a tar archive compressed with zstd (`.pkg.tar.zst`) containing:

- **Binary files** to install
- **`.PKGINFO`** - Package metadata (name, version, dependencies, etc.)
- **`.MTREE`** - Hashed list of files with permissions for integrity verification
- **`.BUILDINFO`** - Information for reproducible builds
- **`.INSTALL`** - Optional post-install/upgrade/remove script
- **`.Changelog`** - Optional changelog maintained by the packager

### The Arch Build System

The Arch Build System (ABS) is a ports-like system for building packages from source:

| Component | Description |
|-----------|-------------|
| **PKGBUILD** | Bash script containing build instructions and metadata |
| **makepkg** | Tool that reads PKGBUILDs and creates packages |
| **pacman** | Package manager for installing/removing packages |
| **devtools** | Tools for building in clean chroots (official packages) |

### Package Repositories

- **Official Repositories** (core, extra, multilib): Pre-built binary packages maintained by Arch developers
- **Arch User Repository (AUR)**: Community-maintained PKGBUILDs (not pre-built)

---

## 2. Prerequisites and Setup

### Install Required Tools

```bash
# Install base development tools (required)
sudo pacman -S base-devel

# Install additional useful tools
sudo pacman -S namcap devtools git
```

The `base-devel` group includes: `autoconf`, `automake`, `binutils`, `bison`, `fakeroot`, `file`, `findutils`, `flex`, `gawk`, `gcc`, `gettext`, `grep`, `groff`, `gzip`, `libtool`, `m4`, `make`, `pacman`, `patch`, `pkgconf`, `sed`, `sudo`, `texinfo`, `which`.

### Configure makepkg (Optional)

Edit `/etc/makepkg.conf` or `~/.makepkg.conf`:

```bash
# Use multiple cores for compilation
MAKEFLAGS="-j$(nproc)"

# Use multiple cores for compression
COMPRESSZST=(zstd -c -z -q -T0 -)

# Enable ccache for faster rebuilds (optional)
# BUILDENV=(... ccache ...)
```

### Set Up a Working Directory

```bash
mkdir -p ~/packages
cd ~/packages
```

---

## 3. Understanding the PKGBUILD File

### What is a PKGBUILD?

A PKGBUILD is a Bash script that defines how to build a package. It contains:

1. **Metadata variables** - Package name, version, description, etc.
2. **Source information** - Where to download source files
3. **Functions** - How to build and package the software

### PKGBUILD Template

Use the official prototype as a starting point:

```bash
cp /usr/share/pacman/PKGBUILD.proto ~/packages/mypackage/PKGBUILD
```

### Minimal PKGBUILD Example

```bash
# Maintainer: Your Name <your.email@example.com>
pkgname=hello-world
pkgver=1.0.0
pkgrel=1
pkgdesc="A simple hello world program"
arch=('x86_64')
url="https://example.com/hello-world"
license=('MIT')
depends=('glibc')
source=("https://example.com/${pkgname}-${pkgver}.tar.gz")
sha256sums=('abc123...')

build() {
    cd "$srcdir/$pkgname-$pkgver"
    ./configure --prefix=/usr
    make
}

package() {
    cd "$srcdir/$pkgname-$pkgver"
    make DESTDIR="$pkgdir" install
}
```

---

## 4. PKGBUILD Variables Reference

### Mandatory Variables

| Variable | Description |
|----------|-------------|
| `pkgname` | Package name (lowercase alphanumerics, `@._+-`). Cannot start with `-` or `.` |
| `pkgver` | Upstream version. No hyphens allowed (replace with `_`) |
| `pkgrel` | Arch-specific release number. Starts at `1`, increment for PKGBUILD fixes |
| `arch` | Supported architectures: `('x86_64')` or `('any')` for arch-independent |

### Recommended Variables

| Variable | Description |
|----------|-------------|
| `pkgdesc` | Brief description (≤80 chars). Don't include package name |
| `url` | Upstream project URL |
| `license` | SPDX license identifier(s): `('GPL-3.0-or-later')`, `('MIT')`, etc. |

### Dependency Arrays

| Variable | Description |
|----------|-------------|
| `depends` | Runtime dependencies (required to run the software) |
| `makedepends` | Build-time only dependencies (not needed at runtime) |
| `checkdepends` | Dependencies for running tests (only needed for `check()`) |
| `optdepends` | Optional dependencies with descriptions: `('python: for scripting support')` |

**Important:** Always list all direct dependencies, even if they're pulled in transitively. Transitive dependencies can change.

### Source Arrays

| Variable | Description |
|----------|-------------|
| `source` | Array of source files (URLs or local filenames) |
| `sha256sums` | SHA-256 checksums for each source (or `'SKIP'` to skip verification) |
| `validpgpkeys` | GPG key fingerprints for signature verification |
| `noextract` | Files to not auto-extract |

**Source URL best practices:**

```bash
# Use variables for maintainability
source=("https://github.com/user/repo/archive/v${pkgver}.tar.gz")

# Rename downloaded files
source=("${pkgname}-${pkgver}.tar.gz::https://example.com/download/${pkgver}.tar.gz")

# Local files (must be in same directory as PKGBUILD)
source=("fix-build.patch"
        "myconfig.conf")
```

### Other Useful Variables

| Variable | Description |
|----------|-------------|
| `provides` | Virtual packages this provides: `('libfoo.so=1-64')` |
| `conflicts` | Packages that conflict with this one |
| `replaces` | Packages this replaces (use sparingly, mostly for renames) |
| `backup` | Config files to backup on upgrade: `('etc/myapp.conf')` |
| `options` | Build options: `('!strip' '!buildflags' 'staticlibs')` |
| `install` | Post-install script filename: `install=${pkgname}.install` |
| `changelog` | Changelog filename |
| `epoch` | Force package to be seen as newer (use sparingly) |
| `groups` | Package groups this belongs to |

### Architecture-Specific Variables

Append `_<arch>` to create architecture-specific overrides:

```bash
source_x86_64=("https://example.com/binary-x86_64.tar.gz")
sha256sums_x86_64=('...')
depends_x86_64=('lib32-glibc')
```

---

## 5. PKGBUILD Functions

### Available Variables in Functions

| Variable | Description |
|----------|-------------|
| `$srcdir` | Directory where sources are extracted |
| `$pkgdir` | Fake root directory for installation (becomes package contents) |
| `$startdir` | Directory containing the PKGBUILD (deprecated, avoid using) |

### prepare() - Source Preparation

Optional. Runs after source extraction. Used for patching and source modifications.

```bash
prepare() {
    cd "$srcdir/$pkgname-$pkgver"
    
    # Apply patches
    patch -Np1 -i "$srcdir/fix-build.patch"
    
    # Generate build files
    autoreconf -fiv
}
```

### pkgver() - Dynamic Version (VCS Packages)

Optional. Used to update `pkgver` from VCS sources. See [VCS Package Guidelines](#7-vcs-package-guidelines).

### build() - Compilation

Optional (but usually needed). Compiles the source code.

```bash
build() {
    cd "$srcdir/$pkgname-$pkgver"
    
    # Standard autotools
    ./configure --prefix=/usr
    make
    
    # Or CMake
    cmake -B build -S . \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr
    cmake --build build
    
    # Or Meson
    arch-meson build
    meson compile -C build
}
```

**Important:** Always use `--prefix=/usr`. Arch packages should never install to `/usr/local`.

### check() - Testing

Optional but recommended. Runs the test suite.

```bash
check() {
    cd "$srcdir/$pkgname-$pkgver"
    make check
    # or: make test
    # or: ctest --test-dir build
}
```

### package() - Installation

**Required.** Installs files into `$pkgdir`.

```bash
package() {
    cd "$srcdir/$pkgname-$pkgver"
    
    # Standard make install
    make DESTDIR="$pkgdir" install
    
    # Install license
    install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
    
    # Install documentation
    install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
    
    # Install config file (add to backup array)
    install -Dm644 myapp.conf "$pkgdir/etc/myapp.conf"
}
```

**Note:** Everything in `$pkgdir` will be packaged. The directory structure under `$pkgdir` becomes the root filesystem structure.

### Split Packages

Build multiple packages from one PKGBUILD:

```bash
pkgbase=myproject
pkgname=('myproject' 'myproject-docs')
# ... other variables ...

package_myproject() {
    pkgdesc="The main application"
    depends=('glibc')
    
    cd "$srcdir/$pkgbase-$pkgver"
    make DESTDIR="$pkgdir" install
}

package_myproject-docs() {
    pkgdesc="Documentation for myproject"
    arch=('any')
    
    cd "$srcdir/$pkgbase-$pkgver"
    install -Dm644 docs/* -t "$pkgdir/usr/share/doc/$pkgbase/"
}
```

---

## 6. Building Packages with makepkg

### Basic Usage

```bash
cd ~/packages/mypackage

# Build the package
makepkg

# Build and install dependencies automatically
makepkg -s

# Build, install deps, and install the package
makepkg -si

# Build and remove makedepends afterward
makepkg -sr

# Clean build directory after successful build
makepkg -c
```

### Useful makepkg Options

| Option | Description |
|--------|-------------|
| `-s, --syncdeps` | Install missing dependencies with pacman |
| `-r, --rmdeps` | Remove makedepends after build |
| `-i, --install` | Install package after building |
| `-c, --clean` | Clean up work files after build |
| `-f, --force` | Overwrite existing package |
| `-C, --cleanbuild` | Remove `$srcdir` before building |
| `-o, --nobuild` | Download and extract only (no build) |
| `-e, --noextract` | Don't extract sources (use existing `$srcdir`) |
| `-R, --repackage` | Repackage without rebuilding |
| `-g, --geninteg` | Generate integrity checksums |
| `--nocheck` | Skip the check() function |
| `--skippgpcheck` | Skip PGP signature verification |
| `--skipchecksums` | Skip checksum verification |
| `-L, --log` | Log build output to file |

### Generate Checksums

```bash
# Generate and append to PKGBUILD
makepkg -g >> PKGBUILD

# Or use updpkgsums (from pacman-contrib)
updpkgsums
```

### Install the Built Package

```bash
# Using pacman
sudo pacman -U mypackage-1.0.0-1-x86_64.pkg.tar.zst

# Or using makepkg -i
makepkg -si
```

---

## 7. VCS Package Guidelines

### Naming Convention

VCS packages must be suffixed with the VCS type:

- `-git` for Git
- `-svn` for Subversion
- `-hg` for Mercurial
- `-bzr` for Bazaar

### VCS Source Format

```bash
# Git (most common)
source=("git+https://github.com/user/repo.git")

# Git with specific branch
source=("git+https://github.com/user/repo.git#branch=develop")

# Git with specific tag
source=("git+https://github.com/user/repo.git#tag=v1.0.0")

# Git with specific commit
source=("git+https://github.com/user/repo.git#commit=abc123")

# SSH URL
source=("git+ssh://git@github.com/user/repo.git")
```

For VCS sources, use `SKIP` for checksums:

```bash
sha256sums=('SKIP')
```

### The pkgver() Function

The `pkgver()` function dynamically generates version numbers from VCS sources.

**Recommended format:** `RELEASE.rREVISION` or `RELEASE.rREVISION.gHASH`

**Git with tags (preferred):**

```bash
pkgver() {
    cd "$srcdir/$pkgname"
    git describe --long --tags --abbrev=7 | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}
# Output: 1.2.3.r5.gabc1234 (5 commits after tag v1.2.3)
```

**Git without tags:**

```bash
pkgver() {
    cd "$srcdir/$pkgname"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short=7 HEAD)"
}
# Output: r150.abc1234 (150 commits, hash abc1234)
```

**Mercurial:**

```bash
pkgver() {
    cd "$srcdir/$pkgname"
    printf "r%s.%s" "$(hg identify -n)" "$(hg identify -i)"
}
```

**Subversion:**

```bash
pkgver() {
    cd "$srcdir/$pkgname"
    printf "r%s" "$(svnversion | tr -d 'A-z')"
}
```

### Complete VCS PKGBUILD Example

```bash
# Maintainer: Your Name <email@example.com>
pkgname=myapp-git
_pkgname=myapp
pkgver=1.2.3.r5.gabc1234
pkgrel=1
pkgdesc="My application (development version)"
arch=('x86_64')
url="https://github.com/user/myapp"
license=('MIT')
depends=('glibc')
makedepends=('git' 'cmake')
provides=("${_pkgname}")
conflicts=("${_pkgname}")
source=("git+${url}.git")
sha256sums=('SKIP')

pkgver() {
    cd "$srcdir/$_pkgname"
    git describe --long --tags --abbrev=7 | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

build() {
    cmake -B build -S "$_pkgname" \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr
    cmake --build build
}

package() {
    DESTDIR="$pkgdir" cmake --install build
    install -Dm644 "$srcdir/$_pkgname/LICENSE" \
        "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
```

### VCS Package Rules

1. **Don't commit pkgver bumps alone** - Only commit when PKGBUILD changes
2. **Use provides/conflicts** - Link to the stable package name
3. **Add VCS tool to makedepends** - `git`, `subversion`, `mercurial`, etc.
4. **pkgrel stays at 1** - Reset when pkgver changes, increment only for PKGBUILD fixes

---

## 8. Testing and Quality Assurance

### Using namcap

namcap analyzes PKGBUILDs and packages for common errors:

```bash
# Check PKGBUILD
namcap PKGBUILD

# Check built package
namcap mypackage-1.0.0-1-x86_64.pkg.tar.zst

# Check installed package
namcap -i mypackage
```

**Common namcap warnings:**

| Warning | Meaning |
|---------|---------|
| `dependency X detected and target Y depends on it` | Missing dependency |
| `dependency X included but already satisfied` | Redundant dependency |
| `ELF file has no RELRO` | Security issue (missing hardening) |
| `Dependency included, but may not be needed` | Possible unnecessary dependency |

**Note:** namcap can produce false positives. Investigate warnings but don't blindly follow all suggestions.

### Building in a Clean Chroot

Building in a clean chroot ensures:
- All dependencies are correctly declared
- No accidental linking against packages on your system
- Reproducible builds

**Using devtools (recommended for AUR maintainers):**

```bash
# Install devtools
sudo pacman -S devtools

# Build in clean chroot
pkgctl build

# Or use the repository-specific script
extra-x86_64-build
```

**Using clean-chroot-manager (alternative):**

```bash
# Install from AUR
yay -S clean-chroot-manager

# Create chroot
sudo ccm c

# Build package
sudo ccm s
```

### Testing the Package

```bash
# List package contents
pacman -Qlp mypackage-1.0.0-1-x86_64.pkg.tar.zst

# View package info
pacman -Qip mypackage-1.0.0-1-x86_64.pkg.tar.zst

# Install and test
sudo pacman -U mypackage-1.0.0-1-x86_64.pkg.tar.zst

# Verify installation
pacman -Ql mypackage
pacman -Qi mypackage

# Test the software actually works
mypackage --version
```

### Checking Dependencies

```bash
# Find libraries linked by an executable
ldd /usr/bin/myapp

# Better alternative
lddtree /usr/bin/myapp

# Find which package provides a library
pacman -Qo /usr/lib/libfoo.so
```

---

## 9. Submitting to the AUR

### Prerequisites

1. **Read the guidelines** - Familiarize yourself with [Arch package guidelines](https://wiki.archlinux.org/title/Arch_package_guidelines)
2. **Create an AUR account** - Register at https://aur.archlinux.org
3. **Set up SSH keys** - Required for pushing packages

### Setting Up SSH Authentication

```bash
# Generate a dedicated SSH key for AUR
ssh-keygen -t ed25519 -f ~/.ssh/aur

# Configure SSH
cat >> ~/.ssh/config << EOF
Host aur.archlinux.org
    IdentityFile ~/.ssh/aur
    User aur
EOF

# Copy public key to AUR profile
cat ~/.ssh/aur.pub
# Paste this into "My Account" > "SSH Public Key" on AUR website
```

### Creating a New AUR Package

```bash
# Clone the (empty) AUR repository
git -c init.defaultBranch=master clone ssh://aur@aur.archlinux.org/pkgname.git
cd pkgname

# You'll see: "warning: You appear to have cloned an empty repository."
# This is expected for new packages

# Add your PKGBUILD
cp ~/packages/mypackage/PKGBUILD .

# Generate .SRCINFO (REQUIRED)
makepkg --printsrcinfo > .SRCINFO

# Add files
git add PKGBUILD .SRCINFO

# Commit
git commit -m "Initial upload"

# Push
git push
```

### The .SRCINFO File

The `.SRCINFO` file is a machine-readable representation of PKGBUILD metadata. **It must be regenerated and committed whenever PKGBUILD changes.**

```bash
# Generate .SRCINFO
makepkg --printsrcinfo > .SRCINFO

# Always commit both files together
git add PKGBUILD .SRCINFO
git commit -m "Update to version X.Y.Z"
git push
```

### AUR Submission Rules

1. **No duplicates** - Don't submit packages already in official repos
2. **Must be useful** - Packages should benefit the community
3. **Must be legal** - Comply with licensing terms
4. **x86_64 only** - Packages must support x86_64
5. **No replaces for AUR** - Don't use `replaces` unless renaming a package
6. **Maintain your packages** - Respond to comments and keep packages updated

### Updating an AUR Package

```bash
cd ~/aur/mypackage

# Update PKGBUILD
# ... edit PKGBUILD ...

# Regenerate .SRCINFO
makepkg --printsrcinfo > .SRCINFO

# Commit and push
git add PKGBUILD .SRCINFO
git commit -m "Update to version X.Y.Z"
git push
```

---

## 10. Package Maintenance

### Responding to Feedback

- Monitor comments on your AUR package page
- Address bug reports and feature requests
- Thank users for helpful feedback
- Don't leave comments for every version update

### Flagging Out-of-Date Packages

If you find an unmaintained package:

1. Flag it as out-of-date with details
2. Email the maintainer if possible
3. After 2 weeks with no response, file an orphan request

### Adopting Orphaned Packages

Orphaned packages can be adopted by any registered AUR user:

```bash
# Clone the existing repository
git clone ssh://aur@aur.archlinux.org/pkgname.git

# Make your changes and push
```

### Deletion Requests

Submit deletion requests for:
- Duplicate packages
- Packages moved to official repos
- Abandoned/broken packages with no maintainer

---

## 11. Best Practices and Common Mistakes

### Do's**Use HTTPS sources** when available  
✅ **Verify sources** with checksums and PGP signatures  
✅ **Use `$pkgname` and `$pkgver` variables** in source URLs  
✅ **List all direct dependencies** even if transitively satisfied  
✅ **Test in a clean chroot** before submitting  
✅ **Use `--prefix=/usr`** for configure scripts  
✅ **Include licenses** in `/usr/share/licenses/$pkgname/`**Use `install` command** instead of `cp` for proper permissions  
✅ **Add backup array** for config files in `/etc`  

### Don'ts**Never install to `/usr/local`** - Arch packages use `/usr`**Never modify `$srcdir`** outside of `prepare()`**Don't use `sudo` in functions** - makepkg handles privileges  
❌ **Avoid custom variables without `_` prefix** - May conflict with makepkg  
❌ **Don't skip checksums** without good reason  
❌ **Don't use `replaces` in AUR packages** - Use `conflicts` instead  
❌ **Don't leave empty directories** in packages  
❌ **Don't include `.git` directories** in packages  

### Common Mistakes

**Wrong:** Installing with DESTDIR missing

```bash
package() {
    make install  # Files go to real system!
}
```

**Right:**

```bash
package() {
    make DESTDIR="$pkgdir" install
}
```

---

**Wrong:** Hardcoded paths

```bash
source=("https://example.com/myapp-1.0.0.tar.gz")
```

**Right:**

```bash
source=("https://example.com/${pkgname}-${pkgver}.tar.gz")
```

---

**Wrong:** Missing direct dependency

```bash
depends=('qt5-base')  # Missing qt5-svg even though app uses it
```

**Right:**

```bash
depends=('qt5-base' 'qt5-svg')  # Both are direct dependencies
```

---

**Wrong:** Version with hyphen

```bash
pkgver=1.0.0-beta1
```

**Right:**

```bash
pkgver=1.0.0_beta1
```

---

## 12. Quick Reference

### Minimal PKGBUILD Checklist

```
☐ pkgname (lowercase, valid characters)
☐ pkgver (no hyphens)
☐ pkgrel=1
☐ pkgdesc (≤80 chars)
☐ arch=('x86_64') or arch=('any')
☐ url
☐ license (SPDX identifier)
☐ depends (all direct runtime deps)
☐ makedepends (build-only deps)
☐ source (with variables)
☐ sha256sums (or valid checksums)
☐ build() or package() function
☐ package() function (required)
```

### Common Commands

```bash
# Build package
makepkg -s

# Build and install
makepkg -si

# Generate checksums
makepkg -g >> PKGBUILD
# or: updpkgsums

# Generate .SRCINFO
makepkg --printsrcinfo > .SRCINFO

# Check PKGBUILD with namcap
namcap PKGBUILD

# Check built package
namcap *.pkg.tar.zst

# Build in clean chroot
pkgctl build

# List package contents
pacman -Qlp *.pkg.tar.zst

# Install local package
sudo pacman -U *.pkg.tar.zst
```

### File Locations

| Location | Purpose |
|----------|---------|
| `/usr/bin/` | Executables |
| `/usr/lib/` | Libraries |
| `/usr/share/` | Architecture-independent data |
| `/usr/share/doc/$pkgname/` | Documentation |
| `/usr/share/licenses/$pkgname/` | License files |
| `/usr/share/man/` | Man pages |
| `/etc/` | System configuration (add to `backup` array) |

### Useful Links

- [Creating packages - ArchWiki](https://wiki.archlinux.org/title/Creating_packages)
- [PKGBUILD - ArchWiki](https://wiki.archlinux.org/title/PKGBUILD)
- [Arch package guidelines](https://wiki.archlinux.org/title/Arch_package_guidelines)
- [AUR submission guidelines](https://wiki.archlinux.org/title/AUR_submission_guidelines)
- [VCS package guidelines](https://wiki.archlinux.org/title/VCS_package_guidelines)
- [makepkg man page](https://man.archlinux.org/man/makepkg.8)
- [PKGBUILD man page](https://man.archlinux.org/man/PKGBUILD.5)

---

*This guide consolidates information from the official Arch Linux Wiki and man pages. For the most up-to-date information, always refer to the official documentation.*